admin管理员组

文章数量:1221949

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

<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 after the link is clicked. Specifically, I need to call a function that checks the frame's src attribute and update the div. If I do a:

$("#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.

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

<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 after the link is clicked. Specifically, I need to call a function that checks the frame's src attribute and update the div. If I do a:

$("#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.

Share Improve this question edited Nov 26, 2022 at 18:45 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Apr 16, 2011 at 8:09 Salman ArshadSalman Arshad 272k84 gold badges442 silver badges533 bronze badges 4
  • 2 If the links move to a different domain name than yours, you won't be able to pull the SRC. – Khez Commented Apr 16, 2011 at 8:11
  • 2 $("#frameTrigger") you are missing # ... just in case – Rafay Commented Apr 16, 2011 at 8:14
  • @Khez: you are right, I now see the problem and therefore have to revise the question. – Salman Arshad Commented Apr 16, 2011 at 8:47
  • @3nigma: I've fixed the typo, but now there is another problem, I'll update the question. – Salman Arshad Commented Apr 16, 2011 at 8:48
Add a comment  | 

3 Answers 3

Reset to default 9

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

$("#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.

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:

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

本文标签: javascriptjQuery quotafterclickquot eventStack Overflow