降低<a href=" ">

时间:2016-01-05 04:29:38

标签: javascript jquery html css

On my website I have multiple href's and I need to add a delay between when they are clicked and when they load. As there are hundreds of hrefs I cant have a separate js function for each one.

The two ways I have researched were, passing the contents of the href to javascript as a variable, such as:

<a href="example1.php">example1</a>
<a href="example2.php">example2</a>
<a href="example3.php">example3</a>
<script>
var href = ### CONTENTS OF SELECTED HREF ###
$(function() {
  $("a").on("click", function(event) {
    event.preventDefault();
    setTimeout(function(){window.location.href = href;}, 1000);
  });        
});

Is it possible to do so using CSS?

1 个答案:

答案 0 :(得分:6)

不,使用CSS(层叠样式表,没办法)是不可行的。你需要使用Javascript。

但是,是的,您不需要为每个a编写不同的函数,只需在点击回调中获取href,然后相应地重定向用户。

$(function() {
  $("a").on("click", function(event) {
    event.preventDefault();
    var href = this.href;
    setTimeout(function(){
        window.location = href;
    }, 1000);
  });        
});