IFrame button click event

后端 未结 4 589
傲寒
傲寒 2020-12-11 12:44

I\'m trying to attach an eventlistener to the \"click\" event of a button on a page in an IFrame. The page in the iframe belongs to the same domain as the parent window.

4条回答
  •  不知归路
    2020-12-11 13:25

    Using jQuery:

    $("#iframe_Id").contents("#button_id").click(functionToRun);
    

    Or without jQuery:

    document.getElementById("iframe_Id").contentWindow.document.getElementById("button_id").addEventListener("click", functionToRun, false);
    

    This will only work if the iframe meets the same origin policy.

提交回复
热议问题