質問

I'm trying to have an anchor tag removed and replaced with a span tag but keep the class attr. I have this code but it is showing "undefined" for the class.

$("#wprmenu_menu").find('li:has(ul) > a').replaceWith(function() {
    var Qthis = $(this);
    var className = Qthis.attr('class');
    return $("<span class=\"" + className + "\">" + Qthis.html() + "</span>");
});

the anchor tag gets removed, and the text inside stays but the class does not transfer over.

役に立ちましたか?

解決

Don't see anything wrong with your code. I made a simply assumption based on your selector. Changed the escape to single quotes as well. Not affecting the outcome. Fiddle

$("#wprmenu_menu").find('li:has(ul) > a').replaceWith(function() {
    var Qthis = $(this);
    var className = Qthis.attr('class');
    return $("<span class='" + className + "'>" + Qthis.html() + "</span>");
});

他のヒント

$('#myButton').click(function(){
    $("#someDiv a").replaceWith(function(){
        return $("<span>" + $(this).html() + "</span>");
    });
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top