簡體   English   中英

如何在滿足jquery中的條件時包括一個js文件

[英]How to include a js file, on satistying the condition in jquery

 <script> jQuery(document).ready(function(){ jQuery(".tabmenu li").click(function(){ var current_tab = jQuery(this).attr("class"); var res = current_tab.replace(" active_tab",""); if(res == 'tabmenu-8') { } }); }); </script> 

我有一個上面寫的腳本。 我想要的是,我想在滿足if條件時包括一個js文件。 我怎樣才能做到這一點? 我嘗試了在Google搜索時發現的不同方法,但沒有任何幫助。

這是我要添加到的腳本

<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>

如果條件,請嘗試以下操作:

var myscript = document.createElement('script');
myscript.setAttribute('src','http://code.jquery.com/jquery-1.9.1.js');
document.head.appendChild(myscript);

從JavaScript添加其他腳本實際上非常容易。 您創建一個腳本元素並設置type和src屬性,然后將其添加到頭部(或正文,這並不重要)

var newScript = document.createElement("script");
newScript.setAttribute("type","text/javascript");
newScript.setAttribute("src","http://code.jquery.com/jquery-1.9.1.js");
document.head.appendChild(newScript);

您可以使用.append()

$('head').append($('<script>').attr('type', 'text/javascript').attr('src', '//code.jquery.com/jquery-1.9.1.js'));

如果您已包含jQuery,則最好使用$ .getScript()

$.getScript("http://code.jquery.com/jquery-1.9.1.js");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM