gpt4 book ai didi

jquery - 如何: Two different jQuery on the same page - The page to be included has the newest jQuery

转载 作者:行者123 更新时间:2023-12-01 08:24:33 25 4
gpt4 key购买 nike

我有一个页面可以随意包含在 PHP 中,并且该页面使用 jQuery 1.4.4。(我什至会使用官方 jQuery 库源中的“最新”,但是......)我无法控制的是用户网站 jQuery 库的版本。或者即使用户有 jQuery。

我已经做了一些测试,并将我的页面包含到具有旧版 jQuery 的网站中 - 并且出现了冲突。

这是要包含的脚本的示例。我无法完成这项工作:

....php 代码....

....html 头.....

    <script type="text/javascript">  var jQ144 = jQuery.noConflict(true);</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
jQ144(document).ready(function($){

$(".helpPlease").hover(function(){
$(this).animate({height:'250px'}, { queue:false, duration:400 });
},function(){
$(".helpPlease").animate({height:'17px'}, { queue:false, duration:400 });
});

...(上面的代码只是举例)...更多代码........

});
</script>

这根本行不通。

有没有办法检查页面是否已经有jQuery?如果较旧 - 激活较新的代码 - 我的页面的 Jquery 库?或者...如何让新的“覆盖”旧的??

还是我做错了什么?我尝试了网络上描述的许多建议,但没有结果。

提前致谢! :)

最佳答案

当您包含 jQuery 时,它采用 $ 变量创建 jQuery 类。 $ 恰好是对 jQuery 的引用。因此,当您包含旧版本/不同版本的 jQuery 时,它实际上是“覆盖”(替换)jQuery 类。

经过测试,这有效:(删除 noConflict 调用中的 true 参数)

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
jQuery.noConflict( )
jQuery144 = jQuery
console.log(jQuery144, jQuery)
</script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
jQuery144(function($) {
console.log("hello", $.fn.jquery)
})
jQuery(function($) {
console.log("world", $.fn.jquery)
})
</script>
</head>
<body>
</body>
</html>

编辑 2:您需要牢记一个重要问题。在内部,jQuery 将自身称为 window.jQuery。当您再次包含 jQuery 时,您将重新定义 window.jQuery,但第一个版本中进行的调用是在运行时绑定(bind)的,而不是在定义时绑定(bind)的,因此可能会出现最旧版本的某些内部调用jQuery 版本实际上将在最新版本中制作。请注意。

关于jquery - 如何: Two different jQuery on the same page - The page to be included has the newest jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4927355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com