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
$
, isjQuery
alias, expressedjQuery
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/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。