2014-02-10 73 views
-1

我如何讓jQuery加載一次,我點擊按鈕。到目前爲止,它在加載頁面時加載。我希望它在點擊按鈕後加載!我嘗試了很多東西,他們沒有工作,到目前爲止請幫助...單擊按鈕時如何獲取jQuery加載?

這個到目前爲止我的代碼爲它

<a href="#image_link" title="PutLockerMedia" id="click">watch</a> 
<div style="display:none"> 
    <div id="image_link"> 
    <div class="imageWrapper"> 
     <div class="image"> 
    <iframe src="http://www.putlocker.com/embed/E25FA20CE8643E5F" width="600" height="360" frameborder="0" scrolling="no"></iframe> 
     </div> 
    </div> 
    </div> 
</div> 


<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#click").fancybox({'scrolling': 'no','titleShow': false,'onClosed': 
     function(){$("#login_error").hide();}}); 
    }); 
</script> 
+0

你不需要在重複問題並重新開始 – JFK

回答

0

如果要動態加載的jQuery您按一下按鈕(它請你解釋「爲什麼」要做到這一點),那麼你可以使用這樣的函數加載它:

function loadScript(sScriptSrc, oCallback) { 
    var oHead = document.getElementsByTagName('head')[0]; 
    var oScript = document.createElement('script'); 
    oScript.type = 'text/javascript'; 
    oScript.src = sScriptSrc; 
    // most browsers 
    oScript.onload = oCallback; 
    // IE 6 & 7 
    oScript.onreadystatechange = function() { 
     if (this.readyState == 'complete') { 
      oCallback(); 
     } 
    } 
    oHead.appendChild(oScript); 
} 

請注意,您的其他jQuery代碼不能被前跑後jQuery是加載。您的代碼看起來是這樣的:

<a href="#image_link" title="PutLockerMedia" id="click">watch</a> 
<div style="display:none"> 
    <div id="image_link"> 
    <div class="imageWrapper"> 
     <div class="image"> 
    <iframe src="http://www.putlocker.com/embed/E25FA20CE8643E5F" width="600" height="360" frameborder="0" scrolling="no"></iframe> 
     </div> 
    </div> 
    </div> 
</div> 


<script type="text/javascript"> 
function loadScript(sScriptSrc, oCallback) { 
    var oHead = document.getElementsByTagName('head')[0]; 
    var oScript = document.createElement('script'); 
    oScript.type = 'text/javascript'; 
    oScript.src = sScriptSrc; 
    // most browsers 
    oScript.onload = oCallback; 
    // IE 6 & 7 
    oScript.onreadystatechange = function() { 
     if (this.readyState == 'complete') { 
      oCallback(); 
     } 
    } 
    oHead.appendChild(oScript); 
} 
function jQueryLoaded() { 
     $("#click").fancybox({'scrolling': 'no','titleShow': false,'onClosed': 
     function(){$("#login_error").hide();}}); 
    }); 
} 

document.getElementById("myButton").addEventListener("click", function() { 
    // load jQuery and when it's loaded, call jQueryLoaded 
    loadScript("jquery.js", jQueryLoaded); 
}, false); 

</script> 

其他參考答案:

How to know if JQuery has finished loading

Javascript multiple Dynamic Insertion

0

試試這個

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#click").click(function fancybox(){ 
          'scrolling': 'no', 
          'titleShow': false, 
          'onClosed': function(){ 
             $("#login_error").hide(); 
             } 
     }); 
    }); 
</script> 
相關問題