简体   繁体   中英

remove tags inside hyperlink using jquery

I try to remove the span tag (auto generated, I cannot remove them) from this hyperlink:

<td class="MyClassID">
   <a href="test.asp?showdetail=&ID=<span>12</span>">test</a>
</td>

My query:

$(".MyClassID").find("span").contents().unwrap();

Won't work... Can someone help me?

我认为这是由ASP.NET生成的跨度。代替asp:Label,请使用asp:Literal。那样,将不会生成任何标记。

Try this:

var reg = /<span>|</span>/i;
$(".MyClassID").find("a").attr(href,$(".MyClassID").find("a").attr(href).replace(reg,''));

try this DEMO

$("a").each(function (){
var id=$(this).attr("href").substring($(this).attr("href").indexOf("<span>"))

var k=$(this).attr("href").substring($(this).attr("href").indexOf("<span>"),0);
    $(this).attr("href",k+$(id).text());
console.log($(this).attr("href"));
});

Fiddle Demo

$(".MyClassID a").attr('href', function (_, old) {
    return old.replace('<span>', '').replace('</span>', '');
});


Problem span tag is not preset in .MyClassID

span is inside the href attribute of a tag.

change replace <span> and </span> with empty string

.attr( attributeName, function(index, attr) )

There is no reason to use regular expressions and using regular expressions on HTML is a bad idea.

The amaxing thing is DOM has textContent/innerText which gives you just the text inside witout the elements. Jquery gives you text() that does this for you.

So you can just grab the text and put it back into the element.

$(".MyClassID").find("a").each(
    function () {
        var anchor = $(this);
        anchor.html( anchor.text() );
    }
);

And if you want, you could also just set the text instead of the html

anchor.text( anchor.text() );

Your span tags are inside the attribute for some bizarre reason. Remove them using replace:

$(".MyClassID").find("a").attr("href", function(i, val) {
    return val.replace("<span>", "").replace("</span>", "");
});

Here is a Fiddle of the above.

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