admin管理员组

文章数量:1220092

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

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!

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

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!

Share Improve this question asked Nov 14, 2011 at 21:20 Eyad FallatahEyad Fallatah 1,9484 gold badges24 silver badges36 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

use the startswith selector.

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

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

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

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

本文标签: javascriptjQuery Remove specific span based on idStack Overflow