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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Vivianluolita/article/details/53689745

核心:

 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.onscroll= 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>



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页