blog icon indicating copy to clipboard operation
blog copied to clipboard

调用iframe子页面中的js函数

Open chenxiaochun opened this issue 8 years ago • 0 comments

假设我们有一个index.html中写了这样一个iframe元素:

<iframe src="child.html" id="iframe" name="iframe"></iframe>
<button id="button">click</button>

<script>
var button = document.getElementById('button');
var child = window.iframe;

button.onclick = function(){
    child.say();
}
</script>

child.html中定义一个js函数,以供父页面进行调用:

function say(){
    alert('hello');
}

chenxiaochun avatar Aug 21 '17 05:08 chenxiaochun