Jquery와 다른javascript 프레임워크가 공존하도록 하기
The jQuery library, and virtually all of its plugins are constrained within the jQuery namespace. As a general rule, "global"objects are stored inside the jQuery namespace as well, so you shouldn't get a clash between jQuery and any other library (like Prototype, MooTools, or YUI).
That said, there is one caveat: By default, jQuery uses "
$
"as a shortcut for "jQuery"OVERRIDING THE
$
-FUNCTION However, you can override that default by calling jQuery.noConflict () at any point after jQuery and the other library have both loaded. For example:
<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').hide();
</script>
</head>
<body></body>
</html>
This will revert $ back to its original library. You'll still be able to use "jQuery"in the rest of your application.
Additionally, there's another option. If you want to make sure that jQuery won't conflict with another library - but you want the benefit of a short name, you could do something like this:
<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').hide();
</script>
</head>
<body></body>
</html>
You can define your own alternate names (e.g. jq, $J, awesomeQuery - anything you want).
Finally, if you don't want to define another alternative to the jQuery name (you really like to use $ and don't care about using another library's $ method), then there's still another solution for you. This is most frequently used in the case where you still want the benefits of really short jQuery code, but don't want to cause conflicts with other libraries.
<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').hide();
</script>
</head>
<body></body>
</html>
This is probably the ideal solution for most of your code, considering that there'll be less code that you'll have to change, in order to achieve complete compatibility.
Also see: [ http://docs.jquery.com/Plugins/Authoring#Custom_Alias_in_plugin_code Custom Alias http://showercurtainrodsrings.com
INCLUDING
JQUERY
BEFORE OTHER LIBRARIES If you include jQuery before other libraries, you may use "jQuery"when you do some work with jQuery, and the "$"is also the shortcut for the other library. There is no need for overriding the
$
-function by calling "jQuery.noConflict ()". <html>
<head>
<script src="jquery.js"></script>
<script src="prototype.js"></script>
<script>
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
jQuery("div").hide();
});
// Use Prototype with $(...), etc.
$('someid').hide();
</script>
</head>
<body></body>
</html>
REFERENCING MAGIC - SHORTCUTS FOR
JQUERY
If you don't like typing the full "
jQuery
"all the time, there are some alternative shortcuts: Reassign jQuery to another shortcut
var $j = jQuery;
(This might be the best approach if you wish to use different libraries)
Use the following technique, which allows you to use
$
inside of a block of code without permanently overwriting $: (function($) { /* some code that uses $ */ })(jQuery)
Note: If you use this technique, you will not be able to use Prototype methods inside this capsuled function that expect
$
to be Prototype's $
, so you're making a choice to use only jQuery in that block. Use the argument to the
jQuery(document).ready(function($) {})
DOM ready event:
jQuery(function($) { /* some code that uses $ */ });
Note: Again, inside that block you can't use Prototype methods.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.