Home > Web Front-end > JS Tutorial > body text

Three kinds of $()_jquery of jQuery

WBOY
Release: 2016-05-16 18:37:42
Original
1169 people have browsed it

1. $() can be $(expression), which is a css selector, Xpath or html element, that is, the target element is matched through the above expression.
For example: the object constructed by $("a") uses a CSS selector to construct a jQuery object - it selects all tags. For example:
$("a").click(function(){...})
is an event triggered when any link on the page is clicked. To be precise, jQuery constructs an object $("a") using the tag
, and the function click() is an (event) method of this jQuery object.

For example, there is this piece of HTML code:

Copy the code The code is as follows:

one



two



three
jQuery



而操作这段HTML的是如下一条语句:
alert($("div>p").html());

$()中的是一个查询表达式,也就是用“div>p”这样一个查询表达式构建了一个jQuery对象,然后的“html()”意思是显示其html内容,也就是上面HTML代码段的[two]。再如:
$("

Hello

").appendTo("body");
$() is a string. Use such a string to construct a jQuery object, and then add this string to .

2. $() can be $(element), which is a specific DOM element. For example, common DOM objects include document, location, form, etc., such as this line of code:
$(document). find("div>p").html());
The document in $() is a DOM element, that is, search for the
element with

in the full text, and display


3. $() can be $(function), which is a shorthand for $(document).ready(). The common form is like this:
$ (document).ready(function(){
alert("Hello world!");
});
Variable operation:
$(function(){
alert(" Hello world!");
});

For selecting elements in HTML documents, jQuery has two methods:
1) Such as $("div>ul a"), its It means the a tag in the ul tag in the div tag
However, there is a difference between $('div>ul') and $('div ul'),


$(' div>ul') looks for