动态设置新增内嵌网页iframe的高度

<style>
    iframe{
        width: 100%;
    }
</style>
<script>
    // 动态设置iframe的高度
    function setIframeHeight(iframe) {
        if (iframe) {
            var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
            if (iframeWin.document.body) {
                iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
            }
        }
    }
    // 判断一个url是否可以访问
    function IsLoad(url,fun){
        $.ajax({
            url: url,
            type: 'GET',
            success: function(response) {
                if($.isFunction(fun)){
                    fun(true);
                }
            },
            error:function () {
                if($.isFunction(fun)){
                    fun(false);
                }
            }
        });
    }

    $(function () {
        $("html").on("click","tr",function() {
            if($(this).next().children().length===1){
                $(this).next().remove();
            }else{
                $("tr").each(function(){
                    if($(this).children().length===1){
                        $(this).remove();
                    }
                });
                var tdnode= $(this).children('td').length;
                // 获取当行第一个表格的值
                var text=$(this).children('td').get(0);
                text=$(text).children(":first").get(0).innerText;
				var that=$(this);
                IsLoad("http://www.santuy-rf.com/santuy/html/"+text+".html" ,function(res){
                    if(res){
                        that.after("<tr><td colspan='"+tdnode+"'><iframe frameborder='0' onload='setIframeHeight(this)' scrolling='no'  src='http://www.santuy-rf.com/santuy/html/"+text+".html' onerror='xError(this)'></iframe></td></tr>");
                    }
                });

            }
        })
    })
</script>

猜你喜欢

转载自blog.csdn.net/qq_36120342/article/details/87511382