如果要動態加載的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
你不需要在重複問題並重新開始 – JFK