jQuery Jquery查找以字符串开头的所有ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5616826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:30:34  来源:igfitidea点击:

Jquery find all ids starting with a string?

jqueryjquery-selectors

提问by baggins

Just wondered how I would search for all the ids starting with "content_" in the whole page and also a way to only find them in a named div called "extra_content". Once i have all the ids i want to hide them.

只是想知道如何在整个页面中搜索以“content_”开头的所有 id,以及一种仅在名为“extra_content”的命名 div 中找到它们的方法。一旦我拥有了所有的 ID,我就想隐藏它们。

Below is an example of what i want to find.

下面是我想找到的一个例子。

<div id="content_1"></div> <-- Find
<div id="content_2"></div> <-- Find
<div id="contet_3"></div>

<div id="extra_content"> 
    <div id="content_extra_1"></div> <-- Find
    <div id="content_extra_2"></div> < -- Find
</div>

Examples would be helpful.

例子会有所帮助。

Thanks

谢谢

回答by David Tang

Use the attribute-starts-with selector:

使用属性开始选择器

$('[id^="content_"]').hide();

To limit the search to elements within extra_content:

将搜索限制为 内的元素extra_content

$('#extra_content [id^="content_"]').hide();