简体   繁体   中英

remove specific href children jquery

My code looks something like that:

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

For some reason, I can't figure out how to get rid of the href-elements, if this special href occurs.

I tried the following:

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

didn't do it.

Also I tried this:

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

What am I missing? I want to keep the image-tags, but unwrap them, so there are no a tags surrounding them.

You need to check if your url start's with specific string, not to be equal to it:

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

by using ^ sign your selector will search for elements that have attribute with values that starts with //index.php?eID=...

Try this

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

You can test the code here .

1) You use w-slide class but there is no such class in example. 2) You want to search href EXACTLY like "//index.php?eID=". To search by part use *= matcher like:

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

Example: http://codepen.io/alkuzad/pen/xbdEYP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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