What is the best way to make links to all web pages inactive?

Possible duplicate:
Override default behavior for link ('a') objects in Javascript

What is the best way to create links on a page only on the home page (without clicks, for example, href=# or href="javascript:void()" based on user action.

I wanted to use only Javascript and CSS for this.

+4
source share
7 answers

try

with jquery

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

vanilla js

  var elements = document.getElementsByTagName("a"); for (var i = 0; i < elements.length; i++) { elements[i].onclick = function () { return false; } } 
+5
source

Css only

 a { pointer-events: none; cursor: default; } 
+9
source

Something like this should work

 var anchors = document.getElementsByTagName('a'); for(var i=0,len=anchors.length;i<len;i++){ anchors[i].href = '#'; } 
+2
source

Here's a great CMS solution using event delegation technology.

 document.onclick = function (e) { e = e || window.event; var element = e.target || e.srcElement; if (element.tagName == 'A') { someFunction(element.href); return false; // prevent default action and stop event propagation } }; 

Demo

+2
source

Start with:

 links = document.getElementsByTagName('a') 

Then:

 for (var i=0; i<links.length; ++i) { // disable a link, for instance like this links[i].href = "javascript:void()" } 
0
source

JQuery

 $("a").on("click",function(e) { e.preventDefault(); // anything you want to do on click }); 
0
source

How about using replaceChild - it will work in all browsers with NS6 / IE6 / Chrome1 / FX1 or so

Demo

Plain JS:

 window.onload=function() { var anchors = document.getElementsByTagName('a'); for(var i=anchors.length-1;i>=0;i--){ var span = document.createElement("span"); span.innerHTML=anchors[i].innerHTML; anchors[i].parentNode.replaceChild(span,anchors[i]) span=null; } } 

Or my first sentence in a comment on the page:

 window.onload=function() { var anchors = document.getElementsByTagName('a'); for(var i=anchors.length-1;i>=0;i--){ anchors[i].onclick=function() { return false } } } 
0
source

Source: https://habr.com/ru/post/1437978/


All Articles