TypeError: $ is not a function แก้อย่างไร?

โดยทั่วๆไปแล้วเวลาเราๆ เขียน jQuery กันมักจะใช้
<script type="text/javascript"> $(document).ready( function() { /* Your Coding */ }); </script>
ทีนี้มันเลยเกิดปัญหา TypeError: $ is not a function ขึ้นมาเพราะมันหา Function $ ไม่เจอ
วิธีที่1. ประกาศตัวแปรซะเลย
<script type="text/javascript"> var $ = jQuery; $(document).ready( function() { // Your coding console.log($('body')); }); </script>
วิธีที่2. หักดิบ ในเมื่อมันใช้ $ ไม่ได้เราก็ใช้ jQuery แทนซะเลย อะไรที่เขียนด้วย $ ก็เปล่ยนเป็น jQuery แทนให้หมด
<script type="text/javascript"> jQuery(document).ready( function() { // Your coding console.log(jQuery('body')); }); </script>
วิธีที่3. ลูกครึ่ง ในเมื่อมันหา Function $ เราก็ประกาศมันซะสิ จะได้ใช้งานได้
<script type="text/javascript"> jQuery(document).ready( function($) { // Your coding console.log($('body')); }); </script>
-
18 ตุลาคม 2557
โดย Admin
0 ความคิดเห็น
ความคิดเห็น