JQuery - Probleme lösen (Troubleshooting): Unterschied zwischen den Versionen
| Zeile 16: | Zeile 16: | ||
<pre> | <pre> | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
| + | /*<![CDATA[*/ | ||
| + | <!-- | ||
( function($) { | ( function($) { | ||
// we can now rely on $ within the safety of our “bodyguard” function | // we can now rely on $ within the safety of our “bodyguard” function | ||
| Zeile 23: | Zeile 25: | ||
//this will fail | //this will fail | ||
$(document).ready( function() { alert('fail?'); } ); | $(document).ready( function() { alert('fail?'); } ); | ||
| + | |||
| + | // --> | ||
| + | /*]]>*/ | ||
</script> | </script> | ||
Quelle: [[http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems]] Zugriff 10/2011 | Quelle: [[http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems]] Zugriff 10/2011 | ||
</pre> | </pre> | ||
Version vom 4. Oktober 2011, 19:51 Uhr
jQuery mit anderen Libaries nutzen
Manchmal gibt es Probleme wenn andere Skripte ebenfalls das Dollar ($) Symbol nutzen.
Typische Fehler sind dann z.B. “$().ready is not a function” “$(document) doesn’t support this property or method”. Oder: “null is null or not an object”
Hier gibt es 2 Möglichkeiten die man probieren kann:
No Conflict Mode
Hier wird das $ Zeichen mit einem anderen frei wählbaren Zeichen ersetzt. Es gibt aber auch ähnliche Möglichkeiten wie beim nächsten Beispiel.
http://api.jquery.com/jQuery.noConflict/
jQuery Scope in anonyme Funktion
Hierbei wird der Code innerhalb einer Anonymen Funktion ausgeführt und in dieser gilt wiederum ein lokaler jQuery Scope. Sie wird also unabhängig vom Rest der Skripte ausgeführt. Dies funktioniert auch wenn jQuery schon im no conflict Mode läuft und zwar ohne Anpassung der Skripte.
<script type="text/javascript">
/*<![CDATA[*/
<!--
( function($) {
// we can now rely on $ within the safety of our “bodyguard” function
$(document).ready( function() { alert("nyah nyah! I’m able to use '$'!!!!"); } );
} ) ( jQuery );
//this will fail
$(document).ready( function() { alert('fail?'); } );
// -->
/*]]>*/
</script>
Quelle: [[http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems]] Zugriff 10/2011