使用jQuery查找文本字符串? [英] Find text string using jQuery?

查看:123
本文介绍了使用jQuery查找文本字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说一个网页有一个我想找到的字符串,例如我是一个简单的字符串".我将如何使用JQuery做到这一点?

Say a web page has a string such as "I am a simple string" that I want to find. How would I go about this using JQuery?

推荐答案

jQuery具有contains方法.这是适合您的代码段:

jQuery has the contains method. Here's a snippet for you:

<script type="text/javascript">
$(function() {
    var foundin = $('*:contains("I am a simple string")');
});
</script>

上面的选择器选择包含目标字符串的任何元素. foundin将是一个包含任何匹配元素的jQuery对象.请参阅以下API信息: https://api.jquery.com/contains-selector/

The selector above selects any element that contains the target string. The foundin will be a jQuery object that contains any matched element. See the API information at: https://api.jquery.com/contains-selector/

使用'*'通配符要注意的一件事是,您将获得所有元素,包括html和body元素,而这可能是您不想要的.这就是为什么jQuery和其他地方的大多数示例都使用$('div:contains("I am a simple string")')

One thing to note with the '*' wildcard is that you'll get all elements, including your html an body elements, which you probably don't want. That's why most of the examples at jQuery and other places use $('div:contains("I am a simple string")')

这篇关于使用jQuery查找文本字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆