使用javascript转义iframe

Ton*_*Nam 3 html javascript iframe

我知道如何逃避iframe.我有一个链接:

<a id="aaa" href="http://localhost/projectManagement/users" target="_parent" >Sign up</a>
Run Code Online (Sandbox Code Playgroud)

通过单击该链接,我将转义iframe

问题是我想在点击按钮而不是链接时转义iframe.

我知道如何通过单击按钮来执行JavaScript,所以我一直在使用javascriopt执行该链接

我试过的东西:

html :(它在iframe内)

<button onClick="tempEvent();" style="width: 500px; height: 50px; margin-top: -15px; font-size: 20px;">
      <b>
          click me              
       </b>
</button>
<div style="height: 15px;">
</div>
<a id="aaa" href="http://localhost/projectManagement/users" target="_parent" >Sign up</a>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

<script>
    function tempEvent()
    {

        clickLink(document.getElementById('aaa'));  // this technique does not work on firefox!
        fireEvent(document.getElementById("aaa"),'click'); // this technique does not work in firefox eather.

        document.getElementById("aaa").onclick(); 
    }

    function clickLink(link) {
        if (document.createEvent) {
            var event = document.createEvent("MouseEvents");
            event.initMouseEvent("click", true, true, window,
                0, 0, 0, 0, 0,
                false, false, false, false,
                0, null);
            link.dispatchEvent(event);
        }
        else if (link.fireEvent) {
           link.fireEvent("onclick");
        }
    }

    function fireEvent(obj,evt){    
        var fireOnThis = obj;
        if( document.createEvent ) {
          var evObj = document.createEvent('MouseEvents');
          evObj.initEvent( evt, true, false );
          fireOnThis.dispatchEvent(evObj);
        } else if( document.createEventObject ) {
          fireOnThis.fireEvent('on'+evt);
        }
    }



</script>
Run Code Online (Sandbox Code Playgroud)

我知道我可以用链接替换按钮,但我只是不明白为什么我无法实现这一点!

编辑

即使我将链接放在按钮内也不起作用!

The*_*iac 7

父母的 Javascript :

function redirectMe(url) {
    window.location = url;
}
Run Code Online (Sandbox Code Playgroud)

子iframe中的 Javascript :

function tempEvent() {
    parent.redirectMe('http://localhost/projectManagement/users');
}
Run Code Online (Sandbox Code Playgroud)

你可以<a>一起绕过所有,并不真正需要整个clickLink/fireEvent.如果您确实需要激活链接上的click事件,请查看jQuery及其click()功能.