开发者

How to disable the hyperlinks in a div

开发者 https://www.devze.com 2023-01-17 00:21 出处:网络
How can I disable all hyperlinks in a div element? I don\'t want any active links in my d开发者_如何学运维iv(editable). jQuery:

How can I disable all hyperlinks in a div element? I don't want any active links in my d开发者_如何学运维iv(editable).


jQuery:

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

Old and considered bad.

$("#myEditableDiv a").click(function(){ return false; });


using javascript you can write a simple method as given below -

function disableLinksByElement(el) {
  if (document.getElementById && document.getElementsByTagName) {
    if (typeof(el) == 'string') {
      el = document.getElementById(el);
    }
    var anchors = el.getElementsByTagName('a');
    for (var i=0, end=anchors.length; i<end; i++) {
      anchors[i].onclick = function() {
        return false;
      };
    }
  }
}

//Call to function as

disableLinksByElement('mydiv');


First grab all the links.

var links = editable.getElementsByTagName('a'); // where "editable" is a var pointing to your div

Then set the onclick to false.

for (var i = 0; i < links.length; i++) {
  var link = links[i];
  link.onclick = function() { return false; };
}
0

精彩评论

暂无评论...
验证码 换一张
取 消