作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个小搜索示例可以工作,但只是第一次。
如何清除 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/
我是一名优秀的程序员,十分优秀!