disable a hyperlink using jQuery

后端 未结 11 1670
悲&欢浪女
悲&欢浪女 2020-11-29 02:06
Click me

I did

$(\'.my-link\').attr(\'disabled\', true);
11条回答
  •  -上瘾入骨i
    2020-11-29 03:02

    Removing the href attribute definitely seems to the way to go. If for some reason you need it later, I would just store it in another attribute, e.g.

    $(".my-link").each(function() {
        $(this).attr("data-oldhref", $(this).attr("href"));
        $(this).removeAttr("href");
    });
    

    This is the only way to do it that will make the link appear disabled as well without writing custom CSS. Just binding a click handler to false will make the link appear like a normal link, but nothing will happen when clicking on it, which may be confusing to users. If you are going to go the click handler route, I would at least also .addClass("link-disabled") and write some CSS that makes links with that class appear like normal text.

提交回复
热议问题