Find all link in a div element and disable them all

空扰寡人 提交于 2019-12-04 21:27:20

问题


Assume that I have some HTML elements like these :

<div id="content_div">
    <span><a href="some_link">Click me</a></span>
    <div> Hello everybody. Click <a href="some_link_else">me</a> to do something else</div>

    <a href="3rd_link"> Oops </a>
</div>

All that I need is get all "a" tags in the #content_div and disable all of them (I don't want user to click on them). How could I do that in Jquery?


回答1:


Try this:

 $("#content_div a").click(function(e) {
   e.preventDefault();
 });



回答2:


I would rely less on jQuery as it might be disabled by the user, so if you want a CSS solution, you can do it like

#content_div {
   pointer-events: none;
   cursor: default;
}

Demo

Edit: To be precise, use this declaration #content_div a




回答3:


$("#content_div a").css({"color":"#888888", cursor: "default"}).click(function(e){
   e.preventDefault();
});

Working Example http://jsfiddle.net/P4Fqq/




回答4:


There are two ways here:

1) $('#content a').css({"pointer-events":"none"});

2) $('#content a').click(function(e){ e.preventDefault(); });



来源:https://stackoverflow.com/questions/17186950/find-all-link-in-a-div-element-and-disable-them-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!