删除特定的href子jquery

时间:2015-01-19 12:10:03

标签: javascript jquery

我的代码看起来像这样:

<a href="//index.php?eID=tx_cms_......">
    <img width="1600" height="400" border="0" alt="" src="/link/to/my.jpg">
</a>

出于某种原因,如果发生这种特殊的href,我无法弄清楚如何摆脱href元素。

我尝试了以下内容:

$(".w-slide a[href='//index.php?eID=']").children('img').unwrap();

没有做到。

我也尝试过这个:

$('w-slide a').each(function()
   {
       if ($(this).attr('href').contains('/eID=tx_cms/'))
           {
               $(this).children('img').unwrap();
           }
   }
});

我错过了什么?我想保留图片标签,但要打开它们,因此周围没有标签。

3 个答案:

答案 0 :(得分:1)

您需要检查您的网址start's with特定字符串是否与其相等:

$(".w-slide a[href^='//index.php?eID=']").children('img').unwrap();
                  ^  

使用^符号,您的选择器将搜索具有以//index.php?eID=...

开头的值的属性的元素

答案 1 :(得分:1)

试试这个

$("a[href*='//index.php?eID=']").children("img").remove();

您可以测试代码here

答案 2 :(得分:0)

1)你使用w-slide类但是在例子中没有这样的类。 2)你想像“//index.php?eID=”一样搜索href。要按部分搜索,请使用* = matcher,如:

$(".w-slide[href*='//index.php?eID']")

实施例: http://codepen.io/alkuzad/pen/xbdEYP

相关问题