互联网技术 · 2024年2月19日

使用jQuery的contains过滤器进行精确匹配的方法

:contains 选择器选取包含指定字符串的元素。

该字符串可以是直接包含在元素中的文本,或者被包含于子元素中。

经常与其他元素/选择器一起使用,来选择指定的组中包含指定文本的元素,如:

$(“p:contains(is)”) 表示选择所有包含 “is” 的元素。

再如:

$(“p:contains(张三)”) 或 $(“p:contains(“张三”)”) 表示选择所有包含 “张三” 的元素。

在该选择器里也可以使用变量来达到选取的目的,如:

$(document).ready(function(){

var ddd=”John”;

$(“div:contains(‘” + ddd + “‘)”).css(“color”, “#f00”);

});

我们也可以将jquery的filter方法和contains方法一起使用来达到更加模糊匹配的目的,如:

$(document).ready(function(){

$(“.box”).filter(“:contains(李)”).css(“color”, “#f00”);

});

文章来源:田珊珊个人博客

来源地址:http://www.tianshan277.com/667.html

$.getJSON(“/section/814.json”,function(data){var h = + data[0][title] + : + data[0][description] + ;$(#section-814).append(h);});

申请创业报道,分享创业好点子。点击此处,共同探讨创业新机遇!

$.getJSON(“/section/637.json”,function(data){var channel_id =window.CHANNEL_ID ||0;var channel,default_channel;$.each(data,function(i,r){if (r.channel_id ==0) {default_channel =r;} if (r.channel_id ==channel_id) {channel =r;return false;} }) channel =channel ?channel :default_channel;if (channel &&channel.thumb) {var h =;$(#section-637).append(h);} });

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册