jQuery to trigger a click on an iFrame?

后端 未结 2 1659
不知归路
不知归路 2020-12-03 05:38

If a user clicks on an image I want that to simultaneously trigger a click on a specified iFrame as well. Is that possible? The image and iFrame are located on the same page

相关标签:
2条回答
  • 2020-12-03 06:17

    I think you needed something like this:-

    $("#image").click(function(){
         var $iframe = $("#iframe").contents();
         $("body", $iframe).trigger("click");
    });
    
    0 讨论(0)
  • 2020-12-03 06:27

    Your jquery image selector is wrong.

    change

    jQuery("input.image")
    

    to

    $("#image")
    

    Then everything will work. You can see it in this fiddle

    Post Edit

    If the question is to trigger a click on one of the elements contained in the iFrame, then replace the

    $("#iframe").click()
    

    with something like:

    $("#iframe").contents().find("a:first").click();
    

    This will only work if the contents of the main page and the iFrame's page are on the same domain. Otherwise browsers will prevent the action as it is cross site scripting. You can see in your example, XSS prevention will occur as demonstrated in this fiddle

    0 讨论(0)
提交回复
热议问题