是否可以在隐藏的div中加载iframe,直到显示div?

ris*_*nst 16 html javascript 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>
Run Code Online (Sandbox Code Playgroud)

css很简单:

#slidingDiv, #slidingDiv_2{
height:300px;
background-color: #e2e2e2;
padding:20px;
margin-top:10px;
border-bottom:30px solid #000000;
display:none;
}
Run Code Online (Sandbox Code Playgroud)

这是外部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);
Run Code Online (Sandbox Code Playgroud)

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

问题,我怎样才能确保div中的iframe只会在div不再隐藏时加载?

Sam*_*son 37

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

<div class="hidden_element">
    <iframe data-src="http://msdn.microsoft.com"></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

显示父级时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");
    });
});
Run Code Online (Sandbox Code Playgroud)

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