In this section we learn jQuery grammar through jQuery , we can select HTML elements and perform the operation.

jQuery syntax

jQuery syntax is customized for selecting HTML elements and performing certain operations on the elements.

Basic syntax format:

$(selector).action()
  • The first is the dollar sign $ , is jQuery alias, expressed jQuery object.
  • selector is a selector used to query HTML elements.
  • action() jQuery operation to be performed on the element.
Example:

For example, let's look at a few examples:

$(this).hide()     // 隐藏当前元素
$("span").hide()   // 隐藏所有<span>元素
$(".xkd").hide()   // 隐藏class="xkd"的所有元素
$("#xkd").hide()   // 隐藏id="xkd"的所有元素

Document ready function

In order to prevent the document from running the jQuery code before it is fully loaded, we need a document ready function:

$(document).ready(function() {
    // jquery代码
});

After adding this function, you can operate on DOM only after the loading of DOM is complete.

If you run the function before the document is fully loaded, the operation may fail, for example:

  • Attempt to hide a non-existent element.
  • Try to get the size of an image that hasn't been loaded yet

There is also a concise way of writing the above function:

$(function() {
    // jquery代码
});

The effect of these two writing methods is the same, but the short form seems more convenient. ,

Link: https://www.9xkd.com/


知否
221 声望177 粉丝

Skrike while the iron is hot.


« 上一篇
jQuery 安装
下一篇 »
jQuery 选择器

引用和评论

0 条评论