$(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);
});
});
第一项
第二项
第二项1
$(".selector:contains('xx')")
contains()只作匹配查找,不够精确,包含xx的selector和包含xxabc的selector都会查到。
解决办法:
?$(".selector:contains('xx')[innerHTML='xx']")
这样将查找内容只有xx的selector。