10

让 Prototype 和 jQuery 一起工作

最近做的一个新的项目,为了赶时间,用了不少 jQuery 的 Plugin,不过问题来了,最后和其他人的东西整合的时候,发现他们用的是 prototype,于是 $() 冲突也就在所难免了,幸好 jQuery 提供了这样的解决方案。

有三种不同的方式:

方法一:

<html>
<head>
<script src=”prototype.js”></script>
<script src=”jquery.js”></script>
<script>
jQuery.noConflict();

// Use jQuery via jQuery(…)
jQuery(document).ready(function(){
jQuery(”div”).hide();
});

// Use Prototype with $(…), etc.
$(’someid’).style.display = ‘none’;
</script>
</head>
<body></body>
</html>

方法二:

<html>
<head>
<script src=”prototype.js”></script>
<script src=”jquery.js”></script>
<script>
var $j = jQuery.noConflict();

// Use jQuery via $j(…)
$j(document).ready(function(){
$j(”div”).hide();
});

// Use Prototype with $(…), etc.
$(’someid’).style.display = ‘none’;
</script>
</head>
<body></body>
</html>

方法三:

<html>
<head>
<script src=”prototype.js”></script>
<script src=”jquery.js”></script>
<script>
jQuery.noConflict();

// Put all your code in your document ready area
jQuery(document).ready(function($){
// Do jQuery stuff using $
$(”div”).hide();
});

// Use Prototype with $(…), etc.
$(’someid’).style.display = ‘none’;
</script>
</head>
<body></body>
</html>

我最终使用的是方法二中的方法。

Technorati : ,

, ,

引用地址:http://www.steadyxp.com/archives/35.html

要说点啥就在这吧