简体   繁体   English

如何删除jQuery中的特定标签?

[英]How to remove specific tag in jquery?

Example: 例:

html = '<font><a>Test Message</a></font>';

html = html.find('font').remove();

Expecting Output: 预期输出:

<a>Test Message</a>

Actually this code is not working. 实际上,此代码无法正常工作。 Please help me to solve my problem. 请帮助我解决我的问题。

As you've a string that contains HTML markup, you can use string replace functions to remove unwanted elements. 当您拥有包含HTML标记的字符串时 ,可以使用字符串替换功能删除不需要的元素。

To remove only <font> and </font> you can use regex here. 要仅删除<font></font> ,可以在此处使用正则表达式。

 html = '<font><a>Test Message</a></font>'; html = html.replace(/<\\/?font>/g, ''); document.getElementById('result').innerText = html; console.log(html); 
 <pre id="result"></pre> 

Regex Explanation: 正则表达式说明:

/<\\/?font>/g matches <font> and </font> strings. /<\\/?font>/g匹配<font></font>字符串。 \\/? will match zero or one / . 将匹配零或一/

Warning: The g flag in the regex will replace all the matches in the string. 警告: regex中的g标志将替换字符串中的所有匹配项。 So, if the string contains more than one font elements, all will be removed. 因此,如果字符串包含多个font元素,则所有font元素都将被删除。

Note: Using jQuery's remove() will also remove the descendants of the <font> . 注意:使用jQuery的remove()还将删除<font>的后代。


You can also use jQuery's html() methods as follow: 您还可以如下使用jQuery的html()方法:

 html = '<font><a>Test Message</a> </font>'; html = $(html).html(); // To show result on page $(document.body).text(html); console.log(html); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

Use jquery unwrap to remove the parent of the a tag font Then append it to a dynamicaly created div and call the html method to get the result try: 使用jquery unwrap删除a标记font的父代,然后将其附加到动态创建的div并调用html方法以获取结果try:

  html =  $('<div>').append($(html).find('a').unwrap()).html();

https://jsfiddle.net/p0acop2k/ https://jsfiddle.net/p0acop2k/

To remove elements and content, there are mainly two jQuery methods you should use: 要删除元素和内容,主要应使用两种jQuery方法:

  • $(html).find(selector).remove() - Removes the selected element (and its child elements) $(html).find(selector).remove() -删除选定的元素(及其子元素)

  • $(html).find(selector).empty() - Removes the child elements from the selected element $(html).find(selector).empty() -从选定元素中删除子元素

Give more detailed code if you need more detailed answer with right selector. 如果您需要使用正确选择器的更详细答案,请提供更详细的代码。

Links to jQuery for remove and empty methods. 链接到jQuery的removeempty方法。

You can use it like this 你可以这样使用

$(html).find('font').remove();

as @Tushar commented Note: This'll also remove the anchor element inside <font> , resulting in giving empty string, 如@Tushar注释:注意:这还将删除<font>内的anchor元素,从而产生空字符串,

so you can use 所以你可以使用

html = '<font><a>Test Message</a></font>';
new_html = $(html).find('a').clone(); // this is just an anchor
// now your new html is <a>Test Message</a>
html = $('<font><a>Test Message</a></font>');
html.find('font').contents().unwrap();
console.log(html.html());

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

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