javascript - 是否可以在显示 div 之前不在隐藏的 div 中加载 iframe?

标签 javascript html hidden

基本上,当客户点击链接时显示的隐藏 div。该 div 显示一个 php formail,询问客户有关他/她感兴趣的产品的问题。

这是我在网上找到的代码,它非常适合我使用 jquery:

<script type="text/javascript">

$(document).ready(function(){


$('.show_hide').showHide({           
    speed: 500,  // speed you want the toggle to happen 
    easing: '',  // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI
    changeText: 1, // if you dont want the button text to change, set this to 0
    showText: 'REQUEST A QUOTE',// the button text to show when a div is closed
    hideText: 'CLOSE' // the button text to show when a div is open

}); 


});

</script>

CSS 很简单:

#slidingDiv, #slidingDiv_2{
height:300px;
background-color: #e2e2e2;
padding:20px;
margin-top:10px;
border-bottom:30px solid #000000;
display:none;
}

这是外部 java 脚本:

(function ($) {
$.fn.showHide = function (options) {

    //default vars for the plugin
    var defaults = {
        speed: 1000,
        easing: '',
        changeText: 0,
        showText: 'Show',
        hideText: 'Hide'

    };
    var options = $.extend(defaults, options);

    $(this).click(function () { 

         $('.toggleDiv').slideUp(options.speed, options.easing);    
         // this var stores which button you've clicked
         var toggleClick = $(this);
         // this reads the rel attribute of the button to determine which div id to toggle
         var toggleDiv = $(this).attr('rel');
         // here we toggle show/hide the correct div at the right speed and using which easing effect
         $(toggleDiv).slideToggle(options.speed, options.easing, function() {
         // this only fires once the animation is completed
         if(options.changeText==1){
         $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
         }
          });

      return false;

    });

};
})(jQuery);

现在我相信在这段代码中,表单会自动加载,即使没有显示。我可能是错的,如果是的话请纠正我。

问题,如何确保 div 内的这个 iframe 仅在 div 不再隐藏时加载?

最佳答案

而不是加载 iframe及其 src属性,用 data-src 加载它属性。对于它的值,请提供您在父级 div 时使用的最终位置。变得可见。

<div class="hidden_element">
    <iframe data-src="http://msdn.microsoft.com"></iframe>
</div>

当您显示父项时 div , 发出一个接受 iframe 的回调属性 data-src , 并将其值设置为 src iframe 的属性,导致它加载。

// Show our element, then call our callback
$(".hidden_element").show(function(){
    // Find the iframes within our newly-visible element
    $(this).find("iframe").prop("src", function(){
        // Set their src attribute to the value of data-src
        return $(this).data("src");
    });
});

演示:http://jsfiddle.net/bLUkk/

关于javascript - 是否可以在显示 div 之前不在隐藏的 div 中加载 iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13437091/

相关文章:

javascript - 在 ReactJS 应用程序中动态重新编译 JSX

html - 显示中的位置 :absolute,:在 IE 中具有动态高度的表格单元格

jquery - 使用 SlideDown 显示 div

javascript - JQuery 视差背景随鼠标悬停移动

javascript - 如何在 NodeJs 或 javascript 中的当前 unix 时间戳中添加 24 小时?

javascript - 根据类名替换开始和结束标记

jquery - FancyBox 画廊集成

javascript - 创建手动幻灯片时出现错误

javascript - 结合Safari浏览器检测和.on ('change' , function() 使用jQuery

android - 隐藏运行 Activity (隐身模式)