[英]Stopping and firing an event in JavaScript
I'm click on a 'a' tag and the page is redirecting at some domain.我点击了一个“a”标签,页面正在重定向到某个域。 Now I have written some function on the 'a' tag click which uses e.preventDefault()
.现在我已经在使用e.preventDefault()
的 'a' 标签点击上写了一些函数。 I'm keeping target safe in another variable using var temp = e.target;
我使用var temp = e.target;
在另一个变量中保持目标安全var temp = e.target;
. .
But when I'm trying to fire it again it is not firing up without any js error.但是当我尝试再次启动它时,它不会在没有任何 js 错误的情况下启动。
Here is the code :-这是代码:-
var allowedReload = false;
var reloadTargetControl = null;
$('a').click(function (e) {
debugger;
if (allowedReload == undefined || allowedReload == false) {
reloadTargetControl = e.target;
e.preventDefault();
pageLeaveConfirmBox(); /*this function is creating custom confirm box with Yes/No button*/
/******* Custom confirm box - yes button code *******/
$("#popUpBtnYes").click(function () {
debugger;
allowedReload = true;
$(reloadTargetControl).click(); /*this step is executing the same script again but nothing is happening, can any one explain it why? */
});
}
});
to trigger an event manually you should use trigger function:要手动触发事件,您应该使用触发功能:
$(reloadTargetControl).trigger('click');
Other than this you could improve your code by changing:除此之外,您可以通过更改以下内容来改进代码:
if (allowedReload == undefined || allowedReload == false)
to到
if (allowedReload === undefined || !allowedReload)
In your #popUpBtnYes click function you set allowedReload
to true, but where is the else
portion of your conditional that would handle that true condition?在您的 #popUpBtnYes 单击函数中,您将allowedReload
设置为 true,但是您的条件的else
部分在哪里可以处理该真实条件? You've sent a click event to the original target but not supplied any expression to handle the changed state.您已将点击事件发送到原始目标,但未提供任何表达式来处理更改的状态。 I think all you need is else { return true; }
我认为你所需要的只是else { return true; }
else { return true; }
in order for this to work. else { return true; }
为了这个工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.