给页面内嵌iframe绑定监听事件

核心:

 var testiframe=document.getElementById("iframetest").contentWindow;

代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
</head>
<body>

<p>屏幕宽度为<span>0</span> px</p>
<p>请试着重新调整浏览器窗口的大小。</p>
<iframe  id="iframetest" width="320" height="330" name="aa" frameborder="2" src="../html/wxPictureContentsEdit.html">1111</iframe>
<!--scrolling="no"-->
<script type="text/javascript">
    $(document).ready(function(){
        function InitialClass(){
            $("iframe").css("width","60%");
            $("body").css("background","pink");}
           /* }*/
        InitialClass();
        $(window).resize(function() {
            var WindowWidth=$(window).width();
            $("span").text(WindowWidth);
            var brow=$.browser;
            var bInfo="";
            if(brow.msie){
                if(WindowWidth>1250){
                    $("iframe").css("width","100%");
                }else if((900<WindowWidth)&&(WindowWidth<1200)){
                    $("iframe").css("width","100%");
                }else if((400<WindowWidth)&&(WindowWidth<900)){
                    $("iframe").css("width","100%");
                }else if((100<WindowWidth)&&(WindowWidth<300)){
                    $("iframe").css("width","100%");
                }
            }
            if(brow.mozilla){}
            if(brow.safari){}
            if(brow.opera){}
        });


        //滚动监听事件
        var testiframe=document.getElementById("iframetest").contentWindow;
        var doc=testiframe.document;
        /*testiframe.scroll(0,doc.body.scrollHeight);*/
        testiframe.addEventListener("scroll",function(e){
            alert("111");
            return stopDefault(e);
        },false)
      /*  testiframe.οnscrοll= function () {
            alert("11111");
        }*/
    });


   /* var testiframe=document.getElementById("iframetest");

    console.log(testiframe);

    var fun = function(){
        alert("333333");
        testiframe.removeEventListener("scroll",fun,false);
    }

    testiframe.addEventListener("scroll",fun,false);*/











</script>
<![endif]-->
</body>
</html>



  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值