javascript-jQuery - IFRAME 呼叫父視窗內的函數 - iframe

假如利用 js 能否呼叫 iframe 中的函數呢? jQuery 提供相關的呼叫父親視窗內的函數功能

test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery - IFRAME 呼叫父視窗內的函數 - parent</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function () {
  callMe = function(str) {
    $('div').text(str);
  }
});
</script>
<body>
<div>我是DIV</div>
<iframe src="167.html" width="300px" height="120px" border="0" frameborder="0"></iframe>
</body>
</html>

167.html
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery - IFRAME 呼叫父視窗內的函數 - iframe</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function () {
  var wParent = window.parent;
  $('button').click(function() {
    wParent.callMe($('div').text());
  });
});
</script>
<body>
<div>我是 IFRAME 裡的字串!!!</div>
<button>按我</button>
</body>
</html>

留言

這個網誌中的熱門文章

c語言-關於#define用法

PHP教學 - 資料型態(Data Type) - 上

CMD常用網管指令