使用 jQuery 查找文本字符串? [英] Find text string using 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屋!