jQuery删除<a> tag from certain sub class

时间:2016-03-18 21:07:56

标签: jquery html

I wanted to remove the <a> tag from certain classes. Here is my html:

<div class="my_class">
    <div class="sub_class">
        <p class="percent">0%</p>
    </div>
    <div>
        <div id="my_links_1">
            <p class="my_url"><a href="#">my link 1</a></p>
            <p class="my_url"><a href="#">my link 2</a></p>
        </div>
    </div>
    <div class="sub_class">
        <p class="percent">50%</p>
    </div>
    <div>
        <div id="my_links_2">
            <p class="my_url"><a href="#">my link 3</a></p>
            <p class="my_url"><a href="#">my link 4</a></p>
        </div>
    </div>
    <div class="sub_class">
        <p class="percent">50%</p>
    </div>
    <div>
        <div id="my_links_3">
            <p class="my_url"><a href="#">my link 5</a></p>
            <p class="my_url"><a href="#">my link 6</a></p>
        </div>
    </div>
    <div class="sub_class">
        <p class="percent">100%</p>
    </div>
    <div>
        <div id="my_links_4">
            <p class="my_url"><a href="#">my link 7</a></p>
            <p class="my_url"><a href="#">my link 8</a></p>
        </div>
    </div>
</div>

So, what I wanted was to remove all the <a> tags except which percentage is 50%. I tried with this code, but it's removed all the <a> tags.

$.each($('.percent'), function (index, value) { 
    var per = $(value).text();
    if(per != "50%"){
        $(".my_url").find("a").contents().unwrap();
    }
});

1 个答案:

答案 0 :(得分:2)

您需要使用适当的DELIMITER
检查以下代码段

&#13;
&#13;
selector
&#13;
$('.percent').each(function() {
  var per = $(this).text();
  if (per != "50%") {
    $(this).parent('div').next().find(".my_url").find("a").contents().unwrap();
  }
});
&#13;
&#13;
&#13;