最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to set this.getAttribute('href') in this javascript? - Stack Overflow

programmeradmin6浏览0评论

I am trying to load a link after 4 seconds after click. Javascript and link are below

 <script>
 function myFunction()
 {
 setTimeout(function(){window.location.assign(this.getAttribute('href'))},4000);
 }
 </script>

<a href="" onclick="myFunction(); return false">

But its not working. How can i solve this. Thanks for your answers.

I am trying to load a link after 4 seconds after click. Javascript and link are below

 <script>
 function myFunction()
 {
 setTimeout(function(){window.location.assign(this.getAttribute('href'))},4000);
 }
 </script>

<a href="http://www.foo." onclick="myFunction(); return false">

But its not working. How can i solve this. Thanks for your answers.

Share Improve this question asked Jan 15, 2014 at 2:52 robintonrobinton 755 silver badges14 bronze badges 1
  • Why do you want to do this and break how browsers work, including Ctrl-click? – Matijs van Zuijlen Commented Dec 19, 2017 at 12:58
Add a ment  | 

1 Answer 1

Reset to default 7

You need to pass the clicked element reference to the click handler

<a href="http://www.foo." onclick="myFunction(this); return false">

then

function myFunction(el) {
    setTimeout(function () {
        window.location.assign(el.getAttribute('href'))
    }, 4000);
}

or using jQuery

function myFunction(el) {
    setTimeout(function () {
        window.location.assign($(el).attr('href'))
    }, 4000);
}
发布评论

评论列表(0)

  1. 暂无评论