1. How does jQuery get elements
The most common requirement for manipulating web elements is to get their values or assign them to them.
You use the same function to do the getter and the setter, so the value and the setter are one and the same. Whether to evaluate or assign depends on the parameters of the function.
`$('h1').html(); ` // HTML () takes no arguments to fetch the value of h1
`$('h1').html('Hello'); ` // HTML () takes the argument Hello, which assigns h1
Copy the code
`[.html()]`Gets or sets the HTML content`[.text()]`Retrieves or sets the text content`[.attr()]`Gets or sets the value of an attribute`[.width()]`Gets or sets the width of an element`[.height()]`Gets or sets the height of an element`[.val()]`Retrieves the value of a form elementCopy the code
2. How is jQuery chained
Once the page element is finally selected, you can perform a series of operations on it, all of which can be linked together as a chain:
$('div').find('h3').eq(2).html('Hello');
- Broken down, it looks like this:
$('div') // Find the div element.find('h3') // Select the h3 element.eq(2) // Select the third h3 element.html('Hello'); // Change its content to Hello
Copy the code
- JQuery also provides the [.end()] method:
$('div'). Find ('h3'). Eq. (2). The HTML ('Hello') * *. The end ()// Return to the step where all h3 elements are selected **.eq(0) // Select the first h3 element.html('World'); // Change its content to World
Copy the code
3. How does jQuery create elements
- Just pass the new element directly into jQuery’s constructor:
$('<p>Hello</p>'); $('<li class="new">new list item</li>'); $('ul').append('<li>list item</li>');
Copy the code
4. How does jQuery move elements
- The first way is to use
[.insertAfter()]
, move the div element after the p element:
$('div').insertAfter($('p'));
The second way is to use [.after()], which precedes the div element with p:
$('p').after($('div'));
-
There are four pairs of operation methods using this mode:
[.insertafter ()] and [.after()] : Insert elements from behind outside of existing elements [.insertbefore ()] and [.before()] : Outside the existing element, insert elements [.appendto ()] and [.append()] from the front: Inside the existing element, insert elements [.prependto ()] and [.prepend()] from the back: Inside the existing element, insert elements from the frontCopy the code
The above quotes from the design idea of jQuery – Ruanyifeng.com
5. How does jQuery modify element attributes
jquery
usingattr()
Methods toGets and sets element attributes, attrattributeAbbreviation for attribute
$(selector).attr(attribute) //attribute Specifies the attribute whose value is to be obtained.
$(selector).attr(attribute,value)//attribute Specifies the name of the attribute. Value Specifies the value of the attribute.
Copy the code