使用JavaScript突出显示/加粗段落中的第n个单词

时间:2018-06-28 10:06:47

标签: javascript html

我是JavaScript的新手,所以做某些事情对我来说似乎很艰巨。


我正在创建一个网页,该网页将包含一个段落(大概3000到5000个单词)。

<! DOCTYPE html>
<html>
<head><title>:: JavaScript is a powerful Language ::</title></head>
<body>

<div id='text' class='paragraph article'>
<p align ='justify'> JS or JavaScript is a powerful Client-Side Programming Language, Using it, we can manupulate the Contents of HTML file without Reloading the Page. JS makes the Page more interactive for the Users.</p>
</div>
</body>
</html>

在这里,我要使文本中的单词出现,使得每一秒之后,单词从右到左依次变粗体,然后变为普通。

就像“ JS”(在上面的代码中,<p>...</P>中)一样,我想使用JS对其加粗1秒钟,然后将其取消加粗,然后将第二个“或”加粗持续1秒钟,然后松开等等。


为此,我做了一个小计时器,它将变量a 更改为每秒 +1 ,我想使用该变量的段落中的第n个单词,并对其进行粗体和粗体


为此,我搜索了StackOverflow和许多网站中的每个链接,但是如果您知道任何类似的功能或任何其他解决方案,我都没有得到任何有利的功能可以做到这一点,请帮忙。


PS-我想在没有任何JQuery的情况下使用Pure JavaScript做到这一点。

1 个答案:

答案 0 :(得分:1)

仅从段落中获取单词,然后在向每个单词添加2018-06-28 14:01:18 [scrapy.core.scraper] ERROR: Spider error processing <GET https://smight.com/en/> (referer: https://smight.com/) Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/utils/defer.py", line 102, in iter_errback yield next(it) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spidermiddlewares/offsite.py", line 30, in process_spider_output for x in result: File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spidermiddlewares/referer.py", line 339, in <genexpr> return (_set_referer(r) for r in result or ()) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spidermiddlewares/urllength.py", line 37, in <genexpr> return (r for r in result or () if _filter(r)) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spidermiddlewares/depth.py", line 58, in <genexpr> return (r for r in result or () if _filter(r)) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spiders/crawl.py", line 76, in _parse_response cb_res = callback(response, **cb_kwargs) or () File "/Users/gnus/Desktop/scraper/scraper/spiders/scraper.py", line 33, in parse_item clean_text = lxml.html.tostring(cleaner.clean_html(lxml.html.parse(body))) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/lxml/html/__init__.py", line 940, in parse return etree.parse(filename_or_url, parser, base_url=base_url, **kw) File "src/lxml/etree.pyx", line 3426, in lxml.etree.parse File "src/lxml/parser.pxi", line 1839, in lxml.etree._parseDocument File "src/lxml/parser.pxi", line 1865, in lxml.etree._parseDocumentFromURL File "src/lxml/parser.pxi", line 1769, in lxml.etree._parseDocFromFile File "src/lxml/parser.pxi", line 1162, in lxml.etree._BaseParser._parseDocFromFile File "src/lxml/parser.pxi", line 600, in lxml.etree._ParserContext._handleParseResultDoc File "src/lxml/parser.pxi", line 710, in lxml.etree._handleParseResult File "src/lxml/parser.pxi", line 637, in lxml.etree._raiseParseError OSError: Error reading file 'https://smight.com/en/': failed to load external entity "https://smight.com/en/" 标记的同时对其进行迭代?

<strong>
function highlight(paragraph, interval = 1000) {
  const words = paragraph.innerText.split(' ');

  words.forEach((word, i, arr) => {
    setTimeout(() => {
      const wordsCopy = [...arr];

      wordsCopy[i] = `<strong>${word}</strong>`;
      paragraph.innerHTML = wordsCopy.join(' ');
      
      if (i === arr.length - 1) {
      	setTimeout(() => {
        	paragraph.innerHTML = arr.join(' ');
        }, interval);
      }
    }, interval * i);
  });
}

const p = document.getElementById('par');
highlight(p);