Javascript jQuery“点击后”事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5685275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:27:35  来源:igfitidea点击:

jQuery "after-click" event

javascriptjqueryjavascript-events

提问by Salman A

I have a list of links that open in an iframe, like:

我有一个在 iframe 中打开的链接列表,例如:

<ul id="frameTrigger">
    <li><a target="iframe1" href="aaa.html"><img src="aaa.jpg"></a></li>
    <li><a target="iframe1" href="bbb.html"><img src="bbb.jpg"></a></li>
    <li><a target="iframe1" href="ccc.html"><img src="ccc.jpg"></a></li>
</ul>

I need to update another div afterthe link is clicked. Specifically, I need to call a function that checks the frame's srcattribute and update the div. If I do a:

单击链接,我需要更新另一个 div 。具体来说,我需要调用一个函数来检查框架的src属性并更新 div。如果我做:

$("#frameTrigger a").click(function() {
    var iframe = $("iframe[name=iframe1]").get(0);
    console.log(iframe.contentWindow.location.href);
    // the problem here is that that the iframe.window.location will
    // change AFTER this function returns
});

I do not get what is expected.

我没有得到预期的结果。

采纳答案by Brad

I'm guessing you get the old value instead of the new one? You may want to wrap your function in a setTimeoutof 1ms to allow the update to go through.

我猜你得到的是旧值而不是新值?您可能希望在 1setTimeout毫秒内包装您的函数以允许更新通过。

回答by Andrew Hymanman

$("#frameTrigger a").click(function(){
    console.log( $(this).attr("href") );
});

You need the first selector to work on clicks for the links. Also, I think you had the syntax for console.log incorrect (though I have never used it). I changed it so when you click on a link it logs the href attribute of the link. Hope this helps.

您需要第一个选择器来处理链接的点击。另外,我认为您的 console.log 语法不正确(尽管我从未使用过它)。我改变了它,所以当你点击一个链接时,它会记录链接的 href 属性。希望这可以帮助。

回答by todd

If you're having trouble with the entire click functionality completing when calling the click via jQuery .click() - I know it's not ideal, but - perhaps you should just have jQuery explicitly complete the sequence that should be executed when clicked. Something like:

如果您在通过 jQuery .click() 调用点击时无法完成整个点击功能 - 我知道这并不理想,但是 - 也许您应该让 jQuery 明确完成点击时应执行的序列。就像是:

$("#someElement").bind('click', function(event) {
    event.preventDefault();
    $($("#frameTrigger a:eq(1)").attr('target')).attr('src', $("#frameTrigger a:eq(1)").attr('href'));
});