문제

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