gpt4 book ai didi

javascript - 如何从 $ ('p' 的集合中删除所有类?

转载 作者:行者123 更新时间:2023-11-30 23:44:32 25 4
gpt4 key购买 nike

这个小搜索示例可以工作,但只是第一次。

如何清除 p 元素中的所有类,以便在第二次搜索时,不显示上次搜索的突出显示内容?

<!DOCTYPE html>
<html>
<head>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('jquery', '1.4.2');
google.setOnLoadCallback(function() {
$('#searchButton').click(function() {

//remove all added classes so we can search again
//jQuery.each($('p'), function() {
//$('#' + this).addClass('');
//}

$('p:contains("' + $('#searchText').val() + '")').addClass('highlight');
});
});
</script>
<style>
p.highlight {
background-color: orange;
}
</style>
</head>
<body>
<input id="searchText" value="second" />
<button id="searchButton">Search</button>
<p>This is the first entry.</p>
<p>This is the second entry.</p>
<p>This is the third entry.</p>
<p>This is the fourth entry.</p>
</body>
</html>

最佳答案

您可以删除所有p 上的highlight 类,然后在匹配的元素上再次执行hightlight。

$('p').removeClass('hightlight');

关于javascript - 如何从 $ ('p' 的集合中删除所有类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3514411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com