[英]How to disable a function in javascript onclick
当刷新按钮或用户尝试退出他们被重定向的页面时,我添加了一个重定向功能,但我想要发生的是当他们单击我的按钮时卸载或不执行此功能
<a onClick="alert(you are going to tweeter);"href="http://twitter.com" class="tweetbutton" >I Like Twitter</a>
所以发生的事情是它发生冲突并显示两个警报,我需要的是当点击 tweetbutton 按钮时,onclick 警报将出现然后重定向到 twitter.com 而没有执行下面的功能
(function() {
setTimeout(function() {
var __redirect_to = 'http://facebook.com';
var _tags = ['btn', 'input'],
_go, _i, _i2;
for (_i in _tags) {
_els = document.getElementsByTagName(_tags[_i]);
for (_i2 in _go) {
if ((_tags[_i] == 'input' && _go[_i2].type != 'btn' && _go[_i2].type != 'submit' && _go[_i2].type != 'image') || _go[_i2].target == '_blank') continue;
_els[_i2].onclick = function() {
window.onbeforeunload = function() {};
}
}
}
window.onbeforeunload = function() {
setTimeout(function() {
window.onbeforeunload = function() {};
setTimeout(function() {
document.location.href = __redirect_to;
});
});
return 'you are leaving this page'
}
});
});
更新您的代码如下:
<a "href="http://twitter.com" class="tweetbutton" >I Like Twitter</a>
将此添加到您的 js
document.addEventListener('click', function (e) {
if (e.target.className == 'tweetbutton') alert('you are going to twitter');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.