简体   繁体   中英

How to remove text between two tags?

I have such a code:

<input type="text">some dummy text i need to remove! <select>F.i.</select>

I tried to to use jQuery for this. But it doesn't seem to work.

$('div').children('select').prev().remove();

It deletes input instead of that text.

So, how to remove text which is simply between tags?

Sometimes it's just simpler to use the native API when working with next nodes.

var select = $("div > select")[0];

select.parentNode.removeChild(select.previousSibling);

You can also make your selector a little more specific if need be.

var select = $("div > input + select")[0];

You could use jQuery to do the remove as well if you did this:

var select = $("div > input + select")[0];
$(select.previousSibling).remove();

which means you could do it in one line:

$($("div > input + select")[0].previousSibling).remove();

but that gets a little hard to read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM