jQuery .trigger('click') 在间隔函数内?
这是来自此处的改写问题。经过一些测试后,我隔离了问题,但没有解决它的线索。无需阅读上一个问题,这是简化的代码:
THE PROBLEM -> trigger('click')
执行,但事件在内部时不会触发循环(间隔)函数
$(document.ready(function(){
var checkForConfirmation = function(){
clearInterval(checkInterval);
$("#anchorLink").trigger('click');
}
var checkInterval = setInterval(function(){checkForConfirmation()}, 5000);
});
该函数按间隔调用。当回复正确的响应时,间隔停止,并模拟锚链接的点击。
在 html 文件中,有一个锚链接 Show
。
它指向有一些内容的隐藏div。我正在使用 Fancybox 插件在单击锚链接时显示隐藏的 div。
如果我点击 Show
链接 fancybox 将按预期显示。
如果我从后端得到响应,代码将按预期执行,但不会显示 fancybox。
如果我将 $("#anchorLink").trigger('click');
移到 checkForConfirmation
函数之外,fancybox 将显示。
当我将 $("#anchorLink").trigger('click');
替换为 $("#anchorLink").text('Im clicked');
时字符串显示在 标记中。
这是总结,我在不同情况下都尝试过。
问题显然是在循环函数中触发 click 事件。 $("#anchorLink")
选择器是可访问的,它可以从其他地方正确触发它。显然,在循环函数内触发鼠标事件存在问题。
有什么建议吗?
This is a rephrased question from here. After some testing I isolated the problem, but have no clue on fixing it. No need to read the previous question, this is the simplified code:
THE PROBLEM -> trigger('click')
executes, but the event doesn't trigger when inside looped (intervaled) function
$(document.ready(function(){
var checkForConfirmation = function(){
clearInterval(checkInterval);
$("#anchorLink").trigger('click');
}
var checkInterval = setInterval(function(){checkForConfirmation()}, 5000);
});
The function is being called in intervals. When the proper response is replied, interval stops, and simulates the click on an anchor link.
In the html file there is an anchor link <a id="anchorLink" href="#hiddenDiv">Show</a>
.
It points to the hidden div which has some content. I'm using Fancybox plugin to show the hidden div when the anchor link is clicked.
If I click on Show
link fancybox shows, as expected.
If I get the response from the back-end, code executes as expected, but fancybox is not shown.
If I move $("#anchorLink").trigger('click');
outside the checkForConfirmation
function, fancybox shows.
When I replace $("#anchorLink").trigger('click');
with $("#anchorLink").text('Im clicked');
the string shows in the <a id="ancoredLink">
tag.
This is the summary, I have tried it in different situations.
The problem is obviously in triggering the click event while in looping function. The $("#anchorLink")
selector is accessible, it is triggering it correctly from everywhere else. Obviously there is a problem in triggering the mouse event inside looping function.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
而不是:
Try:
instead of: