manage iframe's form submit from parent frame?

2019-06-14 02:06发布

问题:

I want to change the submit function of form in iframe from the parent frame. The following code should do that, but nothing hapen. And no error shown in chrome's debugger. What is wrong?

<iframe src="test.php" id="frame"></iframe>

<script type="text/javascript">
$("#frame").contents().find("form").submit(function(){
    return true;
});
</script>

回答1:

Make sure that you are manipulating the DOM of this iframe once its contents has been loaded by wrapping your call inside a .load callback:

<iframe src="test.php" id="frame"></iframe>

<script type="text/javascript">
    $('#frame').load(function() {
        $(this).contents().find('form').submit(function() {
            return false;
        });
    });
</script>


回答2:

You cannot manipulate any of the actions of an iFrame through JS.

That is the whole point of an iFrame.

Try today, try forever. It cannot be done. That is why Facebook "like" buttons and many other big-league items are always contained withing iFrames, so you cannot change what they look like or what they do.