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?
答案 0 :(得分:6)
不,使用CSS(层叠样式表,没办法)是不可行的。你需要使用Javascript。
但是,是的,您不需要为每个a
编写不同的函数,只需在点击回调中获取href
,然后相应地重定向用户。
$(function() {
$("a").on("click", function(event) {
event.preventDefault();
var href = this.href;
setTimeout(function(){
window.location = href;
}, 1000);
});
});