加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

jQuery contains过滤器实现精确匹配使用方法

发布时间:2020-12-14 22:53:04 所属栏目:资源 来源:网络整理
导读:复制代码 代码如下: !DOCTYPE html html xmlns="http://www.w3.org/1999/xhtml" head meta http-equiv="Content-Type" content="text/html; charset=utf-8" / title/title !--script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"

复制代码 代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//根据select中的option的文本来执行选中
//$("#selectbox option[text='第二项']");
//$("#selectbox option").filter("[text='第二项']");
//上面两种写法都是错误的
//正确写法
$("#btn4").click(function () {
var $option =$("#selectbox option:contains('第二项')").map(function(){
if ($(this).text() == "第二项") {
return this;
}
});
alert($option.length > 0 ? "有对象" : "无对象");
$option.attr("selected",true);
});
});
</script>
</head>
<body>
<form id="form1">
<div>
<select id="selectbox">
<option value="1">第一项</option>
<option value="2">第二项</option>
<option value="21">第二项1</option>
</select>
<input type="button" id="btn4" value="contains测试" />
</div>
</form>
</body>
</html>

$(".selector:contains('xx')")
  contains()只作匹配查找,不够精确,包含xx的selector和包含xxabc的selector都会查到。

解决办法:
?$(".selector:contains('xx')[innerHTML='xx']")
  这样将查找内容只有xx的selector。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读