Jquery - 如果链接属性href包含我需要找到的字符串,如何删除链接或解包子元素?

时间:2014-03-25 09:42:55

标签: javascript jquery

我尝试了我的Jquery,但它并没有删除或解开我想要的任何东西。你能看看我的代码并告诉我我做错了什么。非常感谢你。

Jquery的:

$('.link3').each(function(){
      if($('.link3').attr('href').indexOf('139_case-study-distributing-data-from-remote-plants.html') != -1){  
         if($(this).children().lenght > 0){
            $(this).children.unwrap();
         }else{
            $(this).remove();
         }
      }
});

HTML:

<div class="col-2">

    <a class="link3 ui-link" href="/cz/cs/24_produkty-a-partneri/139_case-study-distributing-data-from-remote-plants.html?do=article"> </a>
    <div class="mod-subcats ui-collapsible ui-collapsible-inset ui-collapsible-collapsed" data-theme="d" data-role="collapsible">
    <div class="ui-collapsible-content ui-collapsible-content-collapsed" aria-hidden="true">
      <a class="link3 ui-link" href="/cz/cs/24_produkty-a-partneri/139_case-study-distributing-data-from-remote-plants.html?do=article">
       <h2> ... </h2>
      </a>
      <ul id="mod-links" class="ui-listview ui-listview-inset ui-corner-all ui-shadow" data inset="true" data-theme="d" data-role="listview">
        ..content
      </ul>
    </div>
    </div>
    <br class="clear">

</div>

4 个答案:

答案 0 :(得分:0)

您缺少括号和拼写错误的长度:

$('.link3').each(function(){
      if($('.link3').attr('href').indexOf('139_case-study-distributing-data-from-remote-plants.html') != -1){  
         if($(this).children().lenght > 0){ //should be length
            $(this).children.unwrap(); //here should be children()
         }else{
            $(this).remove();
         }
      }
});

答案 1 :(得分:0)

  

长度拼写错误。在代码中使用长度和缺少括号

  //if($(this).children().lenght > 0){

答案 2 :(得分:0)

更改为

$('.link3').each(function(){
      if($('.link3').attr('href').indexOf('139_case-study-distributing-data-from-remote-plants.html') != -1){  
         if($(this).children().length > 0){
            $(this).children.unwrap(); //here should be children()
         }else{
            $(this).remove();
         }
      }
});

答案 3 :(得分:0)

你可以用css到达那里:

  $('.link3[href*="139_case-study-distributing-data-from-remote-plants.html"] > *').unwrap();
  $('.link3:not([href*="139_case-study-distributing-data-from-remote-plants.html"])').remove();
相关问题