javascript jQuery根据id删除特定跨度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8128269/
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-10-26 02:23:11  来源:igfitidea点击:

jQuery Remove specific span based on id

javascriptjquery

提问by Э?ad Дьdulя?мaи

I want to be able to remove only spans which has an id that stars with the key word "junk_"

我希望能够仅删除具有以关键字“junk_”开头的 id 的跨度

for example if I have

例如,如果我有

<span id="junk_8345">A</span>
<span id="valid_2972">B</span>
<span id="junk_9249">C</span>

The output should look like

输出应该看起来像

<span id="valid_2972">B</span>

I've tried to use

我试过用

$("span:has(junk)").remove();

but it does not seem to work. I'm open to use regex expression with the replace function if I can accomplish the same goal. Any help would be greatly appreciated!

但它似乎不起作用。如果可以实现相同的目标,我愿意将正则表达式与替换函数一起使用。任何帮助将不胜感激!

回答by Jim H.

use the startswithselector.

使用startswith选择器。

$('span[id^="junk"]').remove();

回答by Nordes

Based on what the API page give about "selectors" ( http://api.jquery.com/category/selectors/), I suggest you trying to use the start with ^=.

根据 API 页面提供的关于“选择器”(http://api.jquery.com/category/selectors/)的内容,我建议您尝试使用^=开头。

http://api.jquery.com/attribute-starts-with-selector/

http://api.jquery.com/attribute-starts-with-selector/

$('span[id^="junk"]').remove();