字符串中有一些类似单词,我只需要突出显示一个单词。我有这个特定单词的索引值。我为此使用了Mark JS。我需要在标记JS函数的过滤部分执行此操作。但不知道如何在过滤时获取每个单词的索引值。突出显示来自类似单词的特定单词字符串
这里是fiddle测试
代码JS
$(function() {
$content = $(".content"),
currentIndex = 0;
var indexVal = 40;
$('#markBtn').on("click", function() {
var searchVal = " ipsum ";
$content.unmark({
done: function() {
console.log("unmark")
$content.mark(searchVal, {
"element": "span",
"className": "mark",
"accuracy": "partially",
"iframes": true,
"ignoreJoiners": true,
"acrossElements": true,
"separateWordSearch": true,
"diacritics": false,
"filter": function (textNode, foundTerm, totalCounter, counter) {
console.log("marks - filter " + textNode+" "+foundTerm);
//check indexVal with foundTerm and return true
return true;
},
"each": function (node) {
console.log("marks - each " + node);
},
"done": function() {
console.log("marks - done ");
}
});
}
});
});
});
HTML
<div class="header">
Mark second word 'ipsum' in the text below.
<button id="markBtn">Mark Text</button>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur ipsum adipiscing elit ipsum.</p>
</div>
totalCounter不返回foundTerm – pravid
的索引值返回的值实际上是一个表达式: totalCounter == CURRENTINDEX,这将是一个布尔值 –
告诉我巫比赛数量要标记?如果先是 return totalCounter == 0;如果第二则返回totalCounter == 1;等等...... –