简体   繁体   English

使achor标签一段时间不可点击,然后再次使其可点击

[英]Make an achor tag non-clickable for while and make it clickable again

How can I make an anchor tag clickable after few seconds ? 几秒钟后如何使锚标记可点击? I made it non-clickable but now can't make it clickable again. 我将其设为不可点击,但现在无法再次使其变为可点击。

(Note: there will be no id used for the tag) (注意:该标签将不使用ID)

Heres my html and javascript: 这是我的html和javascript:

function neww(id,time){
    var sec,min,hr;
    var i=(time*1);
    var neew=setInterval(function(){
        if(i>0){
            i--;
            if(i>=60){
                min=parseInt(i/60);
                sec=i%60;         
                if(min>=60){
                    hr=parseInt(min/60);
                    min=min%60;
                }else{
                    hr=0;
                }
            }else{
                min=0;
                hr=0;
                sec=i;
            }
            if(sec<10){
                sec="0"+sec;
            }
            if(min<10){
                min="0"+min;
            }
            if(hr<10){
                hr="0"+hr;
            }
            id.onclick=function(){return false}; // its working here
            id.style.color="red";
            id.style.backgroundColor="#ffffff";
            id.innerHTML=hr+':'+min+':'+sec;
        }
        if(i==0){
            id.innerHTML="Ready";
            id.style.color="#ffffff";
            id.style.backgroundColor="green";
            if(id.onclick==false){id.onclick=function(){return true};} // but its not working
            clearInterval(neew);
        }
    },1000);
}

Html: HTML:

<a href="http://www.google.com/" target="_blank" class="mynewclass" onclick="neww(this,5);">Ready</a>

-Thanks in advance. -提前致谢。

SOLVED: 解决了:

I just removed the 'onclick' attribute from the anchor, so the timer function gets no barrier until the timer completes. 我只是从锚中删除了“ onclick”属性,因此在计时器完成之前,计时器功能不会遇到任何障碍。 Thank you everybody for your effort which helped me to solve this. 感谢大家的努力,这帮助我解决了这一问题。

Thiss for the link is alive but that doesn't interfere the timer function: 这对于链接是有效的,但这不会影响计时器功能:

function neww(id,time){
    var link=id.getAttribute("onclick");
    id.removeAttribute("onclick");
    var sec,min,hr;
    var i=(time*1);
    var neew=setInterval(function(){
        if(i>0){
            i--;
            if(i>=60){
                min=parseInt(i/60);
                sec=i%60;         
                if(min>=60){
                    hr=parseInt(min/60);
                    min=min%60;
                }else{
                    hr=0;
                }
            }else{
                min=0;
                hr=0;
                sec=i;
            }
            if(sec<10){
                sec="0"+sec;
            }
            if(min<10){
                min="0"+min;
            }
            if(hr<10){
                hr="0"+hr;
            }
            id.style.color="red";
            id.style.backgroundColor="#ffffff";
            id.innerHTML=hr+':'+min+':'+sec;
        }
        if(i==0){
            id.innerHTML="Ready";
            id.style.color="#ffffff";
            id.style.backgroundColor="green";
            id.setAttribute("onclick",link);
            clearInterval(neew);
        }
    },1000);
}

And thiss for the link is dead while the timer is running: 计时器运行时,链接的链接已失效:

function neww(id,time){
    var link=id.getAttribute("onclick");
    var linkk=id.getAttribute("href");    
    var sec,min,hr;
    var i=(time*1);//+60;
    var neew=setInterval(function(){
        if(i>0){
            i--;
            if(i>=60){
                min=parseInt(i/60);
                sec=i%60;         
                if(min>=60){
                    hr=parseInt(min/60);
                    min=min%60;
                }else{
                    hr=0;
                }
            }else{
                min=0;
                hr=0;
                sec=i;
            }
            if(sec<10){
                sec="0"+sec;
            }
            if(min<10){
                min="0"+min;
            }
            if(hr<10){
                hr="0"+hr;
            }
            id.removeAttribute("onclick");
            id.removeAttribute("href");
            id.style.color="red";
            id.style.backgroundColor="#ffffff";
            id.innerHTML=hr+':'+min+':'+sec;
        }
        if(i==0){
            id.innerHTML="Ready";
            id.style.color="#ffffff";
            id.style.backgroundColor="green";
            id.setAttribute("onclick",link);
            id.setAttribute("href",linkk);
            clearInterval(neew);
        }
    },1000);
}

Here I am giving you one idea. 在这里,我给你一个主意。 please modify, according to your need. 请根据您的需要进行修改。 Hope it help. 希望对您有所帮助。

After three minutes, it will create the link. 三分钟后,它将创建链接。

HTML: HTML:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <a href="http://www.google.com/" target="_blank" class="mynewclass">Ready</a>
</body>
</html>

jQuery: jQuery的:

$(function(){
  var link = $('.mynewclass').attr('href');
  $('.mynewclass').removeAttr('href');

  setTimeout(function(){
    $('.mynewclass').attr('href', link);
  }, 3000);
});

Javascript: I am using the javascript getElementsByClassName method. Javascript:我正在使用javascript getElementsByClassName方法。 if you are using older browser then i think it will not work. 如果您使用的是较旧的浏览器,那么我认为它将无法正常工作。 please check the browser support. 请检查浏览器支持。

window.onload = function () {
        var elem = document.getElementsByClassName('mynewclass'),
            urlLink = elem[0].getAttribute('href'),
            emptyURL = elem[0].removeAttribute('href');

    setTimeout(function () {
        urlLink = elem[0].setAttribute('href', urlLink);
    }, 3000);
}

Here is the jsbin link - http://jsbin.com/dawit/2/ 这里是jsbin链接- http://jsbin.com/dawit/2/

In Vanilla JavaScript: 在Vanilla JavaScript中:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Delay Click</title>
</head>

<body>
    <a href="http://www.google.com/" target="_blank" class="mynewclass">Ready</a> |
    <a href="http://www.google.com/" target="_blank" class="mynewclass">Ready</a> |
    <a href="http://www.google.com/" target="_blank" class="mynewclass">Ready</a> |

<script>
    var enableLinks = false;

    setTimeout(function(){
        enableLinks = true;
    }, 5000); //add delay of 5 seconds = 5000 miliseconds

    function clickHandler(e){
        var el = e.target;
        if(!enableLinks){
            e.preventDefault();
        }else{
            //add rest of your logic here
            console.log("it's working");
        }   
    }

    var anchors = document.querySelectorAll(".mynewclass");
    for(var i=0; i< anchors.length; i++){
    if (anchors[i].addEventListener) {
      anchors[i].addEventListener('click', clickHandler, false); 
    } else if (anchors[i].attachEvent)  {
      anchors[i].attachEvent('onclick', clickHandler);
    }
}
</script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM