是否可以将整个 jQuery 放入一个新变量中?
Is it possible to place the entirety of jQuery into a new variable?
我有一个加载 jQuery 3.2.1 的网站。为了让我们的一个小部件(由第三方公司提供)工作,它需要 jQuery 1.11.0,它由公司提供的代码加载到单独的 <script>
标签中。
是否可以将 jQuery 的后者旧版本单独加载到新变量中,这样我就可以从需要旧版本 jQuery 的小部件的 JS 中调用它?这可能吗?
是的!
您必须使用 .noConflict()
将 $
以外的变量分配给第二个加载的变量。
console.log("1: "+$.fn.jquery); // Print "1.11.0" in the console
console.log("2: "+jQuery_321.fn.jquery); // Print "3.2.1" in the console
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script>
jQuery_321 = $.noConflict(); // Assing the variable you wish instead of $
</script>
我有一个加载 jQuery 3.2.1 的网站。为了让我们的一个小部件(由第三方公司提供)工作,它需要 jQuery 1.11.0,它由公司提供的代码加载到单独的 <script>
标签中。
是否可以将 jQuery 的后者旧版本单独加载到新变量中,这样我就可以从需要旧版本 jQuery 的小部件的 JS 中调用它?这可能吗?
是的!
您必须使用 .noConflict()
将 $
以外的变量分配给第二个加载的变量。
console.log("1: "+$.fn.jquery); // Print "1.11.0" in the console
console.log("2: "+jQuery_321.fn.jquery); // Print "3.2.1" in the console
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script>
jQuery_321 = $.noConflict(); // Assing the variable you wish instead of $
</script>