简体   繁体   English

如何删除两个标签之间的文本?

[英]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.我试图为此使用jQuery。 But it doesn't seem to work.但它似乎不起作用。

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

It deletes input instead of that text.它删除input而不是该文本。

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.有时,在处理下一个节点时使用本机 API 会更简单。

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:如果您这样做,您也可以使用 jQuery 进行删除:

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.但这有点难以阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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