javascript - How to block an html link without changing href? - Stack Overflow

Say I have a link:<a href="">Example!<a>I want to keep the link intact, but I�

Say I have a link:

<a href="">Example!</a>

I want to keep the link intact, but I'd like an onclick event to decide if the link is clickable or not

<a onclick='stopLight()' href="">Example!</a>

function stopLight() {

    // conditional logic
    // return true or return false
}

so if stopLight() == true, then link works, if stopLight() == false, then link doesn't. Is making this possible without changing the href, because I'd rather not

Say I have a link:

<a href="http://www.example.">Example!</a>

I want to keep the link intact, but I'd like an onclick event to decide if the link is clickable or not

<a onclick='stopLight()' href="http://www.example.">Example!</a>

function stopLight() {

    // conditional logic
    // return true or return false
}

so if stopLight() == true, then link works, if stopLight() == false, then link doesn't. Is making this possible without changing the href, because I'd rather not

Share Improve this question asked Dec 14, 2012 at 16:02 Shai UIShai UI 52k77 gold badges218 silver badges316 bronze badges 2
  • 2 onclick='return stopLight()' is the correct syntax – mplungjan Commented Dec 14, 2012 at 16:06
  • Are you using jQuery/some framework or raw JavaScript? – Jordan S. Jones Commented Dec 14, 2012 at 16:06
Add a ment  | 

4 Answers 4

Reset to default 4

Use return stopLight() for the onclick handler.
As mplungjan suggested in his ment

In order to cut the default action:

stopLight = function (e) {
    window.event.preventDefault();
}​

This will prevent the event from happening.

So you could do:

<a onclick='isStopLight()' href="http://www.example.">Example!</a>

And in the JS

isStopLight= function(){
   if(stopLight()){
      window.event.preventDefault();
   } 
}

stopLight = function () {
    // conditional logic
    // return true or return false
}​

Cheers.

You could do this with jQuery, and the event.preventDefault() method:

$('a.yourLink').on('click',function(e){
  if (stopLight()){ 
    e.preventDefault();
  }
});

if stopLight is true the default behavior of <a> will be prevented, else it executes normal

Plain JS

  • inline
<a onclick='return stopLight()' href="http://www.example.">Example!</a>
  • unobtrusive:
window.onload=function() {
  document.getElementById("stoplight").onclick=function() {
    // conditional logic
    // return true or return false
  }
}

<a id="stoplight" href="http://www.example.">Example!</a>

jQuery

<a id="stoplight" href="http://www.example.">Example!</a>

$(function() {
  $("#stoplight").on("click",function(e) {
    // conditional logic
    if (false condition) e.preventDefault(); // (or return false;) 
  });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744084821a4555973.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信