最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove <h3> by its text content - Stack Overflow

programmeradmin1浏览0评论

I am trying to remove a 'h3' tag from a header list selecting it by text. For example if I have:

html:

<h3>NO CHANGE</h3>
<h3>h3 to be removed</h3>
<h3>NO CHANGE</h3>
<h3>NO CHANGE</h3>
<h3>NO CHANGE</h3>

jQuery:

var h3_text_remove = $('h3').text();
if (h3_text_remove == 'h3 to be removed'){
    $('h3').remove();
//console.log (h3_text_remove);
}

How can I remove just the header with text 'h3 to be removed'?

fiddle: /

I am trying to remove a 'h3' tag from a header list selecting it by text. For example if I have:

html:

<h3>NO CHANGE</h3>
<h3>h3 to be removed</h3>
<h3>NO CHANGE</h3>
<h3>NO CHANGE</h3>
<h3>NO CHANGE</h3>

jQuery:

var h3_text_remove = $('h3').text();
if (h3_text_remove == 'h3 to be removed'){
    $('h3').remove();
//console.log (h3_text_remove);
}

How can I remove just the header with text 'h3 to be removed'?

fiddle: http://jsfiddle/q2nb08t6/7/

Share Improve this question edited Jun 12, 2015 at 13:28 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jun 12, 2015 at 13:25 SNosSNos 3,4705 gold badges46 silver badges97 bronze badges 1
  • You can use each for h3 tag and checked the text of h3 tag if matched with your condition. then remove the h3 – Umesh Sehta Commented Jun 12, 2015 at 13:27
Add a ment  | 

5 Answers 5

Reset to default 4

You can use contains() selector

$("h3:contains('h3 to be removed')").remove();

Fiddle

Use:

//If text can have leading or trailing spaces
$('h3').filter(function() {
    return $(this).text().trim() == 'h3 to be removed';
}).remove();

//or if the text should match exactly:
$('h3').filter(function() {
    return $(this).text() == 'h3 to be removed';
}).remove();

DEMO 1

$('h3').filter(function() {
    return $(this).text().trim() == 'h3 to be removed';
})
.remove();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>NO CHANGE</h3>
<h3>h3 to be removed</h3>
<h3>NO CHANGE</h3>
<h3>NO CHANGE</h3>
<h3>NO CHANGE</h3>
<h3>h3 to be removed </h3>
<h3> h3 to be removed</h3>
<h3>h3 to be removed xx</h3>

DEMO 2

you can use this solution: basic solution to understand easily.

fiddle: http://jsfiddle/q2nb08t6/10/

$('h3').each(function(){
var h3_text_remove = $(this).text().trim();

if (h3_text_remove == 'h3 to be removed'){
    $(this).remove();
}
});

If you just want to remove the tag and not the content

$('h3').each(function(){
    var h3_text_remove = $(this).text().trim();

    if (h3_text_remove == 'h3 to be removed'){
        $(this).unwrap();
    }
});

You can do this in one line using the :contains() selector:

$('h3:contains("h3 to be removed")').remove();

Or using a variable:

var h3_text_remove = $('#specificH3').text();
$('h3:contains("' + h3_text_remove + '")').remove();

Note that this will remove all h3 elements which contain that text in any way, so both of the following would be removed:

<h3>h3 to be removed</h3>
<h3>foo h3 to be removed bar</h3>

To restrict it to elements containing the whole text only, use filter():

$('h3').filter(function() {
    return $(this).text() == 'h3 to be removed';
}).remove()
发布评论

评论列表(0)

  1. 暂无评论