我想在加载link.php 4秒后为第1帧创建一个自动点击事件。我的Link.php代码是
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Source</title>
<script type='text/javascript' src="js/jquery-1.5.2.min.js"></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$('[alt="Skip"]').click(function(){});
setTimeout(function(){$('[alt="Skip"]').trigger('click');}, 4e3);
});
});//]]>
</script>
</head>
<frameset rows="200px,*" style="width:100%">
<frame src="skip.php" frameborder="0" name="frame1" />
<frame src="http://xyz.com" frameborder="0" />
</frameset><noframes></noframes>
</body>
</html>
在skip.php里面的代码
<!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>Untitled Document</title>
</head>
<body>
<a href="http://www.example.com/"><img src="images/skip.png" border="0" alt="Skip"></a>
</body>
</html>
当javascript代码粘贴在skip.php中时,它正在工作,但我想在link.php中使用它
答案 0 :(得分:2)
工作脚本是
$(document).ready(function(){
$($('frame[name="frame1"]')[0].contentWindow).load(function() {
var documentOfFrame = $('frame[name="frame1"]')[0].contentDocument;
$('[alt="Skip"]',documentOfFrame).click(function(){});
setTimeout(function(){$('[alt="Skip"]',documentOfFrame).trigger('click');}, 4e3);
});
});
答案 1 :(得分:0)
您需要将框架的文档指定为jQuery()
选择器的上下文:
$(function() {
//Waiting till the frame's hierarchy will be constructed
$(frames['frame1']).load(function() {
var documentOfFrame = this.contentDocument;
$('[alt="Skip"]',documentOfFrame).click(function(){});
setTimeout(function(){$('[alt="Skip"]',documentOfFrame).trigger('click');}, 4e3);
});
});
请参阅"Selector Context"部分。