Javascript 在 iframe 父窗口中触发点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3521947/
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 05:00:27  来源:igfitidea点击:

trigger click event in iframe parent window

javascriptjquery

提问by AMIT

<div id="main">
    <a id="mainp" >Parent Window</a>

    <iframe id="iframeindex" src="iframeindex.html" >

    </iframe>
</div>

<script>
     $(function() {
        $('#mainp').live('click', function(e) {
            alert("hi");
            e.preventDefault();
        });

iframeindex source:

iframeindex 来源:

<a id="child1" class="test" href="a.html">aa</a>    

<script>
    $(function() {          
        $('.test').click(function(e){           
            $('#mainp', window.parent.document).css('color', 'red').trigger('click');

            e.preventDefault();
        })
    });

CSS is getting applied on parent window link but click event is not getting trigger. How can i send trigger event to parent window.

CSS 正在应用于父窗口链接,但单击事件未触发。如何将触发事件发送到父窗口。

回答by Mark Schultheiss

window.parent.$('#mainp').trigger('click');

or

或者

window.parent.$('#mainp').click();

回答by Chiwda

I have successfully used the following format where other options didn't work.

我已经成功地使用了以下格式,而其他选项不起作用。

    if ($("#maip", window.parent.document).length > 0){
        $("#mainp", window.parent.document).click();