Jquery 如何找到包含精确匹配单词的div
我已检查以下链接: 但一旦找到div,我就必须设置属性:Jquery 如何找到包含精确匹配单词的div,jquery,html,exact-match,Jquery,Html,Exact Match,我已检查以下链接: 但一旦找到div,我就必须设置属性: $('#viewer .textLayer div:contains('+hotspot_draw_join+')').each(function () { $(this).attr('class','secondtypehotspot secondtypehotspot_ref'); $(this).attr('id',hotspotstr); $(this)
$('#viewer .textLayer div:contains('+hotspot_draw_join+')').each(function () {
$(this).attr('class','secondtypehotspot secondtypehotspot_ref');
$(this).attr('id',hotspotstr);
$(this).attr('onclick','secondtypehotspotfn(event)');
});
如何使用“过滤器”选项执行此操作?您可以这样尝试。它工作正常
<div id="id">
<p>John</p>
<p>Johny</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var results = $('#id').find('*').filter(function () {
if ($(this).text() === 'John') {
$(this).attr("data-val", "hello");
return $(this).text() === 'John';
}
});
results.css('color', 'red');
</script>
约翰
约翰尼
var results=$('#id').find('*').filter(函数(){
如果($(this).text()=='John'){
$(this.attr(“data val”,“hello”);
返回$(this.text()=='John';
}
});
结果:css(‘颜色’、‘红色’);