在JavaScript中选择节点项

时间:2019-01-01 22:01:39

标签: javascript dom

我试图构建一个报价生成器,并且试图从HTML段落的集合中获取报价。

当我尝试访问节点列表中的随机项时,我得到的所有节点都是一堆段落而不是一个段落。

这是我尝试的随机化器:

const quotes = quotesdocument.querySelectorAll("p");

const randomize = function() {
  for(quote of quotes) {
    let num = (Math.floor(Math.random() * Math.floor(quotes.length)) - 1);
    console.log(quotes.item(num));
  }
}

这是我要随机化的HTML的摘录:

<p>&#8220;<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.&#8221;</p>
<p>&#8220;Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.&#8221;</p>
<p>&#8220;We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable for us.&#8221;</p>
<p>&#8220;Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.&#8221;</p>
<p>&#8220;Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.&#8221;</p>

我希望只得到其中一个段落,但我一直都得到它们。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

它不起作用的原因是因为您要遍历所有引号。删除for循环并更改随机化逻辑将解决此问题:

const quotes = document.querySelectorAll("p");

const randomize = function() {
  let num = Math.floor(Math.random() * quotes.length) - 1;
  console.log(quotes.item(num).innerText);
}

randomize();
<p>&#8220;<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.&#8221;</p>
<p>&#8220;Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.&#8221;</p>
<p>&#8220;We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable for
  us.&#8221;</p>
<p>&#8220;Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.&#8221;</p>
<p>&#8220;Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.&#8221;</p>

在上面的代码段中,我console.log()在元素中插入 text ,以便可以看到它的工作原理,但是要访问元素本身,只需删除{{1} }我放在那儿。

答案 1 :(得分:0)

不需要代码中的for..of循环。只需使用已有的代码并将num用作quotes数组索引值即可。我添加了按钮来演示该函数如何仅返回单个值:

function randomQuote() {
  const quotes = document.querySelectorAll("p");
  const num = (Math.floor(Math.random() * Math.floor(quotes.length)));
  return quotes[num].innerText;
}

document.querySelector('#buttonEl').addEventListener('click', () => {
  document.querySelector('#quoteEl').innerHTML = randomQuote();
});
#quoteEl {
  color: red;
}
<input id="buttonEl" type="button" value="Click for a random quote from the list below" />
<div id="quoteEl"></div>
<p>&#8220;<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.&#8221;</p>
<p>&#8220;Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.&#8221;</p>
<p>&#8220;We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable for
  us.&#8221;
</p>
<p>&#8220;Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.&#8221;</p>
<p>&#8220;Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.&#8221;</p>