如何使用Javascript在屏幕上呈现随机选择的div中的文本?

e5nszbig  于 2023-03-28  发布在  Java
关注(0)|答案(3)|浏览(137)

我是Javascript的新手,如果我指的东西不正确,我很抱歉!我有以下脚本,当点击一个按钮时,随机循环通过一个想法列表并落在一个上。这里有一个视频显示它是如何工作的:https://streamable.com/rsojyu
我想让它这样,当脚本降落在选定的想法,想法(即文本-A网站...等“,呈现在左侧边栏。我想它是“粘”,因为在,当我点击按钮,拿出一个新的想法,我希望新选择的想法呈现在一个新的一行下,以前选择的想法。
现在,我的脚本有一些问题。我不确定如何在脚本所处的特定idea div中呈现文本。虽然脚本确实在侧边栏中呈现了一个想法,但它总是列表中的第一个想法,并且这些想法没有堆叠-它似乎被冻结了(即当我再次单击按钮运行脚本时,新的想法不会呈现在前一个想法的下方。)
有谁知道我该怎么解决这个问题吗?

function getIdea() {

  var numElements = $('.idea').length;
  // var verse = $('.idea'.isolate);
  var randomNum = Math.floor(Math.random() * numElements);
  $("button").prop("disabled", true);

  var t = 0;
  var repeat = setInterval(function() {
    randomNum = Math.floor(Math.random() * numElements);
    $('.idea').css("background", "none");
    $('.idea:nth-child(' + randomNum + ')').css("background", "skyblue");
    t += 1;

    if (t == 5) {
      clearInterval(repeat)
      $("button").prop("disabled", false);
      var text = document.getElementById("isolate").textContent;
      document.getElementById("hellomessage").innerHTML = text;

    }
  }, 300);

}
<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Body -->
<div id="ideas">
  <div class="idea" id="isolate">A website where you can make a wish</div>
  <div class="idea" id="isolate">A website that you can rewrite</div>
  <div class="idea" id="isolate">A website that feels analog</div>
  <div class="idea" id="isolate">A website where you perform a ritual</div>
  <div class="idea" id="isolate">A website that is a single image</div>
  <div class="idea" id="isolate">A website with no concept</div>
  <div class="idea" id="isolate">A website that feels illegal</div>
  <div class="idea" id="isolate">A website that hosts banned materials</div>
  <div class="idea" id="isolate">A website playing you music from a boombox</div>
  <div class="idea" id="isolate">A website that's only a concept</div>
  <div class="idea" id="isolate">A website that feels like a B movie</div>
  <div class="idea" id="isolate">A website that keeps growing</div>
  <div class="idea" id="isolate">A website that keeps freezing</div>
  <div class="idea" id="isolate">A website with a timer</div>
  <div class="idea" id="isolate">A website that's running out of time</div>
  <div class="idea" id="isolate">A website with a traffic jam</div>
  <div class="idea" id="isolate">A website that feels like a walkable city</div>
  <div class="idea" id="isolate">A website that other people are walking over</div>
  <div class="idea" id="isolate">A website that loops</div>
  <div class="idea" id="isolate">A website that forgets you</div>
  <div class="idea" id="isolate">A website about your last dream</div>
  <div class="idea" id="isolate">A website wishlist</div>
  <div class="idea" id="isolate">A website with links to other websites</div>
  <div class="idea" id="isolate">A website to distract you for three minutes</div>
  <div class="idea" id="isolate">A website with your personal announcements</div>
  <div class="idea" id="isolate">A website that you need to make smaller</div>
  <div class="idea" id="isolate">A website wikipedia</div>
  <div class="idea" id="isolate">A website that's a platformer</div>
  <div class="idea" id="isolate">A website that you need to highlight to read</div>
  <div class="idea" id="isolate">A website that feels like a campfire</div>
  <div class="idea" id="isolate">A website that you can adjust the temperature of</div>
  <div class="idea" id="isolate">A website where you can smell the zaza</div>
  <div class="idea" id="isolate">A website that's a screensaver</div>
</div>
kcwpcxri

kcwpcxri1#

既然你看起来是新的编程,我会给予你一个更详细的答案。
如果可能的话,出于可访问性和SEO的原因,你应该使用正确的语义标签。这些想法实际上是一个想法的列表,因此你应该使用ul这样的列表。你可以通过使用list-style: none来隐藏CSS中的列表样式。
1.你需要两个按钮。一个按钮随机选择一个想法,一个按钮复制该想法到顶部的另一个列表。然而,如果你以前没有选择过一个想法,你就不能复制一个想法。这就是为什么你需要隐藏添加按钮,一旦一个想法被选中,你就可以显示它。你可以通过给添加按钮一个类来隐藏它。然后在JS中使用classList.remove删除该类。
1.你需要随机选择一个想法。因为我已经起诉了一个语义正确的列表,每个idea都必须是一个列表项,我可以在JS中选择每个idea,方法是使用querySelectorAll,然后选择每个li,它是id为#ideas =〉querySelectorAll('#ideas > li')的元素的子元素。然后我使用随机数函数,从该列表中选择一个随机元素,但需要向其添加+1,因为它从0开始计数。
1.然后,我简单地添加一个类classList.add()到随机选择的元素。这里我可以再次使用querySelector,它允许我以与CSS相同的方式选择元素。在我的例子中,我将其与:nth-child()的使用相结合。
1.我可以使用cloneNode()方法复制或克隆整个节点,然后使用appendChild()函数将其插入另一个列表

let selectedIdea;

document.querySelector('#random').addEventListener('click', function() {
  let ideas = document.querySelectorAll('#ideas > li');

  // removes the highlight from all elements
  ideas.forEach(el => el.classList.remove('highlight'));

  // randomly select a random element
  let randomIdea = Math.floor(Math.random() * ideas.length) + 1;

  // highlight that idea
  selectedIdea = document.querySelector(`#ideas > li:nth-child(${randomIdea})`);
  selectedIdea.classList.add('highlight');

  // makes the add button visible
  document.querySelector('#add').classList.remove('d-none');
})

document.querySelector('#add').addEventListener('click', function() {
  // clones the selected Node
  let clone = selectedIdea.cloneNode(true);

  // adds the clone to new list
  document.querySelector('#helloMessages').appendChild(clone);
})
.d-none {
  display: none;
}

#ideas .highlight {
  background-color: skyblue;
}
<ol id="helloMessages"></ol>

<div>
  <button id="random">Randomly Select an idea</button>
  <button id="add" class="d-none">Add the idea to the list</button>
</div>

<ul id="ideas">
  <li>A website where you can make a wish</li>
  <li>A website that you can rewrite</li>
  <li>A website that feels analog</li>
  <li>A website where you perform a ritual</li>
  <li>A website that is a single image</li>
  <li>A website with no concept</li>
  <li>A website that feels illegal</li>
  <li>A website that hosts banned materials</li>
  <li>A website playing you music from a boombox</li>
  <li>A website that's only a concept</li>
  <li>A website that feels like a B movie</li>
  <li>A website that keeps growing</li>
  <li>A website that keeps freezing</li>
  <li>A website with a timer</li>
  <li>A website that's running out of time</li>
  <li>A website with a traffic jam</li>
  <li>A website that feels like a walkable city</li>
  <li>A website that other people are walking over</li>
  <li>A website that loops</li>
  <li>A website that forgets you</li>
  <li>A website about your last dream</li>
  <li>A website wishlist</li>
  <li>A website with links to other websites</li>
  <li>A website to distract you for three minutes</li>
  <li>A website with your personal announcements</li>
  <li>A website that you need to make smaller</li>
  <li>A website wikipedia</li>
  <li>A website that's a platformer</li>
  <li>A website that you need to highlight to read</li>
  <li>A website that feels like a campfire</li>
  <li>A website that you can adjust the temperature of</li>
  <li>A website where you can smell the zaza</li>
  <li>A website that's a screensaver</li>
</ul>
b09cbbtk

b09cbbtk2#

document.getElementById("hellomessage").innerHTML = text;

每次调用getIdea()时,将替换hellomessage id'd元素中的消息,您应该给予以下操作

document.getElementById('hellomessage').insertAdjacentHTML('beforeend', html_of_idea);

你总是得到第一个答案,因为下面的行返回第一个id为isolate的元素,这是你的第一个答案。

var text = document.getElementById("isolate").textContent;

此外,HTML id属性是一个全局唯一的标识符。因此,您不应该使用相同的id两次。
更新:(请注意,这是不理想的,在我看来,但它的工作):

function getIdea() {

var numElements = $('.idea').length;
// var verse = $('.idea'.isolate);
var randomNum = Math.floor(Math.random() * numElements);
$("button").prop("disabled", true);

var t = 0;
var repeat = setInterval(function() {
  randomNum = Math.floor(Math.random() * numElements);
  $('.idea').css("background", "none");
  $('.idea:nth-child(' + randomNum + ')').css("background", "skyblue");
  t += 1;

  if (t == 5) {

    clearInterval(repeat)
    $("button").prop("disabled", false);
    var text = document.getElementById("isolate").textContent;

    let element = $('.idea').get(randomNum  - 1);

    document.getElementById('hellomessage').insertAdjacentHTML('beforeend', element.innerHTML);

  }
}, 300);

}
vs91vp4v

vs91vp4v3#

首先,你不应该在html中为多个元素使用id。检查这段代码是你所期望的吗?我使用aync和await来做到这一点。

const getIdea = () => {
  const ideas = document.querySelectorAll(".idea");

  const ideasNumber = ideas.length;

  const randomArray = [
    Math.floor(Math.random() * ideasNumber),
    Math.floor(Math.random() * ideasNumber),
    Math.floor(Math.random() * ideasNumber),
    Math.floor(Math.random() * ideasNumber),
    Math.floor(Math.random() * ideasNumber),
  ];

  const randomElements = randomArray.map((item) => ideas[item])

  highlightSequentially(randomElements, 200)
};

const highlightSequentially = async(elements, delay) => {
  for (let i = 0; i < elements.length; i++) {
    elements[i].classList.add("bg-blue");
    await new Promise((resolve) => setTimeout(resolve, delay));
    elements[i].classList.remove("bg-blue");
  }
};
.bg-blue {
  background-color: lightblue;
}

#ideas {
  display: flex;
  flex-wrap: wrap;
  column-gap: 8px;
  row-gap: 8px;
}

.idea {
  display: inline;
  border-radius: 16px;
  border: 1px solid black;
}
<button onclick="getIdea()">Get Idea</button>
<div id="ideas">
  <div class="idea">A website where you can make a wish</div>
  <div class="idea">A website that you can rewrite</div>
  <div class="idea">A website that feels analog</div>
  <div class="idea">A website where you perform a ritual</div>
  <div class="idea">A website that is a single image</div>
  <div class="idea">A website with no concept</div>
  <div class="idea">A website that feels illegal</div>
  <div class="idea">A website that hosts banned materials</div>
  <div class="idea">A website playing you music from a boombox</div>
  <div class="idea">A website that's only a concept</div>
  <div class="idea">A website that feels like a B movie</div>
  <div class="idea">A website that keeps growing</div>
  <div class="idea">A website that keeps freezing</div>
  <div class="idea">A website with a timer</div>
  <div class="idea">A website that's running out of time</div>
  <div class="idea">A website with a traffic jam</div>
  <div class="idea">A website that feels like a walkable city</div>
  <div class="idea">A website that other people are walking over</div>
  <div class="idea">A website that loops</div>
  <div class="idea">A website that forgets you</div>
  <div class="idea">A website about your last dream</div>
  <div class="idea">A website wishlist</div>
  <div class="idea">A website with links to other websites</div>
  <div class="idea">A website to distract you for three minutes</div>
  <div class="idea">A website with your personal announcements</div>
  <div class="idea">A website that you need to make smaller</div>
  <div class="idea">A website wikipedia</div>
  <div class="idea">A website that's a platformer</div>
  <div class="idea">A website that you need to highlight to read</div>
  <div class="idea">A website that feels like a campfire</div>
  <div class="idea">A website that you can adjust the temperature of</div>
  <div class="idea">A website where you can smell the zaza</div>
  <div class="idea">A website that's a screensaver</div>
</div>

相关问题