【问题标题】:Jquery and frame click eventJquery和框架点击事件
【发布时间】:2012-07-04 16:51:29
【问题描述】:

我想在加载 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 中使用它

【问题讨论】:

  • link.php中设置超时函数中的4e3是什么?
  • 它的 4 秒超时功能
  • 在skip.php alt="skip" not alt="跳过这个广告"

标签: jquery click frame frameset


【解决方案1】:

工作脚本是

$(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);
});
});

【讨论】:

    【解决方案2】:

    您需要将框架的文档指定为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" 部分。

    【讨论】:

    • @user1502071 试试这些$(document).ready(function(){ $($('frame[name="frame1"]')[0].contentWindow).load(function(){ alert('frame is loaded'); }); });$(window).load(function(){ alert('main content is loaded'); });。然后告诉我alerts被触发的顺序。
    • @user1502071 尝试用我的答案代码替换alert('frame is loaded');(当然没有$(function() {}))。
    • @user1502071 我提供了跨浏览器和更优雅的解决方案。查看我的更新。
    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 2010-09-07
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多