作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何更改 javascript 以便所有关键字都可以在内容中自动突出显示。目前,当用户点击关键词时,关键词会高亮显示。我想自动突出显示它。但我不知道如何更改 javascript。
<h3>Keywords</h3>
<ul id="keywords">
<li><span>AC</span> <span>Motors<span></li>
<li><span>Adjustable</span> <span>Speed</span> <span>Motor</span> <span>Drives<span></li>
</ul>
<h3>Sentences</h3>
<table style="font-size: 0.9em;">
<tr>
<td class="context">columbus fan & machine corp, dc industrial sales company, ac motors, dc motors, electric motor repair, electrical product servicing, cooling blowers, cooling blower filters, line/load reactors, electrical products, electric product distributors, power products, dc drives, electrical enclosures, electrical generators, electrical heaters, electric pumps, proximity switches, electrical transformers, electrical tachometers</td>
</tr>
<tr>
<td class="context">Columbus Fan & Machine corp is a <span class="featured-word">manunfacturer</span> of top quality Cooling Blowers and Filters along with other related motor accessories for all makes of AC & DC Motors</td>
</tr>
</table>
JavaScript:
$(document).ready(function () {
var context = document.querySelectorAll('.context')
, keywords = document.querySelector('#keywords');
keywords.addEventListener('click', function (event) {
var target = event.target;
for(var i = 0; i < context.length; i++) {
var item = context[i]
, text = item.textContent
, featuredWords = item.querySelectorAll('.featured-word')
, words = Array.prototype.slice.call(featuredWords, 0).map(function(node) {
return node.textContent;
})
, regex = new RegExp('\\b(' + target.textContent + ')\\b', 'ig');
text = text.replace(regex, '<span class="highlight">$1</span>');
// put the bolded words back
words.forEach(function(word) {
text = text.replace(word, '<span class="featured-word">'+word+'</span>');
});
item.innerHTML = text;
}
}, false);
});
CSS:
.highlight {
background-color: yellow;
}
.featured-word {
color: red;
font-weight: bold;
}
最佳答案
与其在关键字的“点击”事件上突出显示,不如简单地在窗口加载时对所有关键字进行突出显示,如下所示,
$(document).ready(function() {
var context = document.querySelectorAll('.context'),
keywords = document.querySelectorAll('ul#keywords li span');
Array.from(keywords).forEach(function(entry) {
for (var i = 0; i < context.length; i++) {
var item = context[i],
text = item.innerHTML,
featuredWords = item.querySelectorAll('.featured-word'),
words = Array.prototype.slice.call(featuredWords, 0).map(function(node) {
return node.textContent;
})
, regex = new RegExp('\\b(' + entry.innerHTML + ')\\b', 'ig');
text = text.replace(regex, '<span class="highlight">$1</span>');
// put the bolded words back
words.forEach(function(word) {
text = text.replace(word, '<span class="featured-word">' + word + '</span>');
});
item.innerHTML = text;
}
}, false);
});
关于javascript - 如何突出显示内容中的所有关键字而不是通过单击突出显示? JSP HTML Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40601416/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!