>  기사  >  웹 프론트엔드  >  상위 페이지에서 하위 페이지의 JS 메서드를 호출합니다.

상위 페이지에서 하위 페이지의 JS 메서드를 호출합니다.

高洛峰
高洛峰원래의
2016-12-24 16:56:281351검색

오늘 하루 종일 작업한 끝에 드디어 온라인에서 해결책을 찾았습니다

참고: 문제는 상위 페이지에서 하위 페이지를 호출하는 방식입니다. . . . .

상위 페이지: parent.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>parent</title>
<script>
 function parentFunction() {
  alert(&#39;function in parent&#39;);
 }
 function callChild() {
  child.window.childFunction();
  /*
   child 为iframe的name属性值,
   不能为id,因为在FireFox下id不能获取iframe对象
  */
 }
</script>
</head>
<body>
<input type="button" name="call child"  value="call child" onclick="callChild()"/>
<br/><br/>
<iframe name="child" src="./child.html" ></iframe>
</body>
</html>

하위 페이지: child.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>child</title>
<script>
 function childFunction() {
  alert(&#39;function in child&#39;);
 }
 function callParent() {
  parent.parentFunction();
 }
</script>
</head>
<body>
<input type="button" name="call parent" value="call parent" onclick="callParent()"/>
</body>
</html>

필요에 따라 해당 코드를 수정할 수 있습니다


상위 페이지에서 하위 페이지를 호출하는 JS 방식에 대한 더 많은 글은 PHP 중국어 홈페이지를 참고해주세요!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.