尝试使用http://headjs.com已经有一段时间了,但我仍然没有使用,因为我不能完全理解有限的文档和示例。如果你能用一些HeadJS给出一些jQuery的例子,那就太好了。
目前,这就是我所拥有的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
</head>
<body>
.....
.....
<script src="js/jQuery.plugins.1.js"></script>
<script src="js/jQuery.plugins.2.js"></script>
<!--
Below are inline and cannot be a separate JS
file because of CMS and templating limitation
//-->
<script>
jQuery.noConflict();
jQuery(document).ready(function($) {
$('.class').someEffect();
// Too many more code here
});
</script>
<!-- End inline scripts //-->
</body>
</html>我的问题:
如果我要使用jQuery.noConflict();,我现在应该把放在哪里?或者,即使我将使用另一个JS库,也不会再有冲突了吗?
谢谢。
发布于 2011-02-17 20:03:27
我觉得你错过了JS的重点。下面是您的<head>元素应该是什么样子:
<head>
<meta charset="utf-8" />
<title>Title</title>
<script src="path/to/head.js"></script>
<script>
// files are loaded in parallel and executed in order they arrive
head.js('https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js')
.js('path/to/other/external.js'),
.js('and/so/on');
// Call a function after all scripts have been loaded and the document is scriptable
head.ready(function ()
{
jQuery.noConflict();
// do your inline stuff with $ (Prototype, not jQuery)
});
</script>
</head>发布于 2011-02-17 20:15:29
给你:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Title</title>
<script src='/js/head.js'></script>
<!--
Below are inline and cannot be a separate JS
file because of CMS and templating limitation
-->
<script>
head.js('https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', 'http://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js', '/js/jQuery.plugins.1.js', '/js/jQuery.plugins.2.js', function($, $P) {
return function() {
// $: jQuery
// $P: Prototype
$('.class').someEffect();
// …
};
}(jQuery, _$));
</script>
</head>
<body>
<!-- your HTML code goes here -->
</body>
</html>我会在那里解释一些事情。
head.js(…, function($, $P) {
return function() {
…
};
}(jQuery, _$));它几乎与匿名函数相同,只是它们不会立即执行。在本例中,它在加载所有脚本时执行。
如果在一个已经使用jQuery的站点上加载$,它会将原始$存储在_$变量中,这就是为什么它可以用jQuery.noConflict()恢复原始$的原因。
noConflict: function( deep ) {
window.$ = _$;
if ( deep ) {
window.jQuery = _jQuery;
}
return jQuery;
},https://stackoverflow.com/questions/5033878
复制相似问题