根据各种用户输入,我们将单词数组放入隐藏的 div (#words) 中,然后使用该信息执行函数。
我想做的是检查 div 中是否存在现有单词,即:
terms = $("#words").html();
然后,在页面上其他位置的可见且单独的 div (.ocrText) 中,仅将这些单词包装在强标记中。
$('.ocrText').each(function() {
$(this).html($(this).html().replace(/term/g, "<strong>term</strong>"));
});
因此,如果他们搜索“Tallant France”并且我们存储了该搜索结果,则会出现以下句子:
“马克·塔兰特曾在法国服役。”
会变成:
“马克 <strong>
Tallant </strong>
服务于 <strong>France</strong>
。”
但我不知道如何将该变量注入(inject)到 .replace()
///
编辑:术语按以下格式插入到 #words div 中: [“Tallant”、“法国”、“ war ”] ...等等。
最佳答案
$('.ocrText').each(function() {
var term = 'term'
var pattern = RegExp(term, 'g')
$(this).html($(this).html().replace(pattern, "<strong>" + term + "</strong>"));
});
关于javascript - 将变量单词包裹在一个跨度中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11764294/