23
2009
การใช้งาน jQuery ร่วมกับ Other Libraries
เนื่องจาก JavaScript Library (jQuery, Prototype, MooTools, YUI or etc.) ต่างก็มี shortcut function คือ $-function เหมือนกัน จะทำอย่างไรละครับทีนี้ถ้าเราจะใช้ Libraries อื่นๆ ร่วมกับ jQuery
1. กรณีที่ โหลดตัว jQuery ตามหลัง Other Libraries
กรณีนี้เราจะต้อง overriding $-function โดยการเรียก jQuery.noConflict() และตำแหน่งที่เรียก jQuery.noConflict() จะนำไปไว้ตรงไหนก็ได้แต่จะต้องวางไว้หลัง jQuery และ Other Libraries ทำการโหลด
ส่วน shortcut function ของ jQuery คุณสามารถใช้ jQuery แทน $ และ shortcut function ของ Other Libraries ก็ให้ใช้ $ เหมือนเดิม
ตัวอย่าง 1.1
<html>
<head>
<script src="jquery.js"></script>
<script src="prototype.js"></script>
<script type="text/javascript">
jQuery.noConflict();
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
jQuery("div").hide();
});
// Use Prototype with $(...), etc.
$('someid').hide();
</script>
</head>
<body></body>
</html>
ตัวอย่าง 1.2
<html>
<head>
<script src="jquery.js"></script>
<script src="prototype.js"></script>
<script type="text/javascript">
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>
2. กรณีที่ โหลดตัว jQuery ก่อน Other Libraries
กรณีนี้ไม่ต้อง overriding $-function โดยการเรียก jQuery.noConflict()
ส่วน shortcut function ของ jQuery และ shortcut function ของ Other Libraries ก็ให้ใช้เหมือนกรณีแรก
ตัวอย่าง
<html>
<head>
<script src="jquery.js"></script>
<script src="prototype.js"></script>
<script type="text/javascript">
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
jQuery("div").hide();
});
// Use Prototype with $(...), etc.
$('someid').hide();
</script>
</head>
<body></body>
</html>
Related Posts
Tags
Facebook Development
Google+ Page
Categories
- HTML5 & CSS3 (8)
- jQuery 1.4 (9)
- jQuery 1.5 (6)
- .data (1)
- jQuery 1.6 (3)
- .data (1)
- jQuery 1.7 (4)
- Events (1)
- jQuery Mobile (3)
- jQuery UI (3)
- Mobile (1)
- News (8)
- Others (28)
- Plugin (15)
- The basics of jQuery (9)
- Tips (13)
- คู่มือการใช้งาน jQuery ฉบับ ภาษา ไทย (30)
- Ajax (4)
- Attributes (1)
- Core (1)
- Deferred (3)
- Effects (7)
- Events (3)
- Manipulation (2)
- Selectors (5)
- Traversing (2)
- Utilities (2)

An article by







