Home >Web Front-end >JS Tutorial >5 Useful Basic jQuery Syntax
<span>$(document).ready(function(){ </span> <span>$(".btn1").click(function(){ </span> <span>$("p").hide(); </span> <span>}); </span> <span>$(".btn2").click(function(){ </span> <span>$("p").show(); </span> <span>}); </span><span>});</span>LIVE DEMO: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_eff_show_hide
<span>$(document).ready(function(){ </span> <span>$(".btn1").click(function(){ </span> <span>$("p").fadeOut() </span> <span>}); </span> <span>$(".btn2").click(function(){ </span> <span>$("p").fadeIn(); </span> <span>}); </span><span>});</span>LIVE DEMO: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_eff_fadeout_fadein
<span>$(document).ready(function(){ </span> <span>$("#start").click(function(){ </span> <span>$("div").animate({height:300},3000); </span> <span>}); </span> <span>$("#stop").click(function(){ </span> <span>$("div").stop(); </span> <span>}); </span><span>});</span>LIVE DEMO: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_eff_stop
<span>$(document).ready(function() </span><span>{ </span> <span>$("#btn1").click(function(){ </span> <span>$("#box").animate({height:"300px"}); </span> <span>}); </span> <span>$("#btn2").click(function(){ </span> <span>$("#box").animate({height:"100px"}); </span> <span>}); </span><span>});</span>LIVE DEMO: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_eff_animate
<span>$(document).ready(function(){ </span> <span>$("button").click(function(){ </span> <span>$("p").slideToggle(); </span> <span>}); </span><span>});</span>LIVE DEMO: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_eff_slidetoggle
The basic syntax of jQuery is designed to select HTML elements and perform some action on the element(s). It is written as $(selector).action(). Here, the dollar sign ($) defines or accesses jQuery, the (selector) finds HTML elements, and the .action() is then performed on the element(s). For example, $(“p”).hide() – hides all paragraph elements.
The document ready event is a crucial part of working with jQuery. It ensures that the JavaScript code doesn’t run until the document is fully loaded. This is important because if your jQuery code runs before the document is fully loaded, it may not work properly. The syntax for this is $(document).ready(function(){ // jQuery methods go here… });
The $ sign is an alias for jQuery. It’s used to define or access jQuery. If you’re using another JavaScript library that uses the $ sign, you can use jQuery.noConflict() to avoid conflicts.
jQuery provides a powerful way to select elements using the same syntax as CSS. You can select elements by tag name, class name, id, attributes, or their relative position in the HTML. For example, to select all paragraph elements, you would use $(“p”).
Once you’ve selected elements, you can perform actions on them using jQuery methods. For example, you can use the .hide() method to hide selected elements, or the .show() method to display them. These methods are called on the selected elements, like so: $(“p”).hide().
Yes, one of the powerful features of jQuery is the ability to chain actions together. This means you can perform multiple actions on the same set of elements, all within a single statement. For example, $(“p”).css(“color”, “red”).slideUp(2000).
jQuery events are actions that can be detected by your application. These include user actions like clicks or key presses, or things like the document loading. You can respond to these events with functions, allowing you to create dynamic, interactive web applications.
Comments in jQuery are written the same way as in JavaScript. You can use // for a single line comment, or /* */ for a multi-line comment. Comments are ignored by the browser and can be used to make notes or explain your code.
To include the jQuery library in your HTML, you need to add a script tag pointing to the jQuery source file. This can be a local copy, or you can link to a version hosted on a CDN. The script tag should be placed in the head of your HTML, or right before the closing body tag.
jQuery is a library built with JavaScript. It provides a simpler, more user-friendly API for working with HTML documents, handling events, creating animations, and much more. While you can do all these things with plain JavaScript, jQuery makes it easier and works across a multitude of browsers.
The above is the detailed content of 5 Useful Basic jQuery Syntax. For more information, please follow other related articles on the PHP Chinese website!