该jQuery.noConflict()方法将$标识符的控制权返回给其他库。以下示例(第10行)中的jQuery代码将在将jQuery加载到页面后立即将其置于无冲突模式,并分配一个新的变量名$j来替换$别名,以避免与原型框架发生冲突。
例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery noConflict() Demo</title>
<script src="js/prototype.js"></script>
<script src="js/jquery.js"></script>
<script>
// Defining the new alias for jQuery
var $j = jQuery.noConflict();
$j(document).ready(function(){
// Display an alert message when the element with ID foo is clicked
$j("#foo").click(function(){
alert("jQuery is working well with prototype.");
});
});
// Some prototype framework code
document.observe("dom:loaded", function(){
// Display an alert message when the element with ID bar is clicked
$("bar").observe("click", function(event){
alert("Prototype is working well with jQuery.");
});
});
</script>
</head>
<body>
<button type="button" id="foo">Run jQuery Code</button>
<button type="button" id="bar">Run Prototype Code</button>
</body>
</html>
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jQuery