如何在满足jquery中的条件的情况下包含js文件

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

本文关键字:条件 情况下 js 文件 包含 满足 jquery      更新时间:2023-09-26
<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文件。我该怎么做?我尝试了在谷歌搜索时发现的不同方法,但没有任何帮助。

这是我想添加到的脚本

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

在if条件下尝试此操作:

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");