且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery:包含选择器替代?

更新时间:2022-12-06 12:45:59

您可以使用 .filter()以找到完全匹配,:包含选择器也返回部分匹配

You can use .filter() to find an exact match, :contains-selector returns partial matches also

var theText = "Large";
$("#size option").filter(function () {
    return $.trim($(this).text()) == theText;
}).attr('selected', 'selected');

演示:小提琴