Get elements
.eq(index) Get a specific jQuery object in the jQuery object collection by index
.eq(-index) Get a jQuery object collection in reverse order by index A specific jQuery object
$( "li" ).eq( 2 ).css( "background-color", "red" );
.get(index) Gets the DOM object of a specific index in the jQuery collection object (automatically converts the jQuery object into a DOM object)
console.log( $( "li" ).get( -1 ) );
.get() Convert jQuery collection object to DOM collection object and return
console.log( $( "li" ).get() );
.index() / .index(selector)/ .index(element) Find specific element index# from the given collection
##1. If there is no parameter, return the index of the first element2. If the parameter is a DOM object or jQuery object, return the index of the parameter in the collection3. If the parameter Is the selector, returns the first matching element index, if not found, returns -1var listItem = $( "#bar" ); alert( "Index: " + $( "li" ).index( listItem ) );
$( ".hello" ).clone().appendTo( ".goodbye" );
$( "li.item-a" ).parent('ul').css( "background-color", "red" );
$( "span.selected" ) .parents( "div" ) .css( "border", "2px red solid" )
$( ".inner" ).append( "<p>Test</p>" ); $( "body" ).append( $newdiv1, [ newdiv2, existingdiv1 ] ); $( "p" ).append( "<strong>Hello</strong>" ); $( "p" ).append( $( "strong" ) ); $( "p" ).append( document.createTextNode( "Hello" ) );
.appendTo(target) Insert the object at the end of the target element. The target element can be selector, DOM object, HTML string , element collection, jQuery object;
$( "h2" ).appendTo( $( ".container" ) ); $( "<p>Test</p>" ).appendTo( ".inner" );
$( ".inner" ).prepend( "<p>Test</p>" );
$( "<p>Test</p>" ).prependTo( ".inner" );
$( ".inner" ).before( "<p>Test</p>" ); $( ".container" ).before( $( "h2" ) ); $( "p" ).first().before( newdiv1, [ newdiv2, existingdiv1 ] ); $( "p" ).before( "<b>Hello</b>" ); $( "p" ).before( document.createTextNode( "Hello" ) );
$( "h2" ).insertBefore( $( ".container" ) );
$( ".inner" ).after( "<p>Test</p>" ); $( "p" ).after( document.createTextNode( "Hello" ) );
$( "<p>Test</p>" ).insertAfter( ".inner" ); $( "p" ).insertAfter( "#foo" );
<div class="container"> <div class="inner">Hello</div> <div class="inner">Goodbye</div></div>
$( ".inner" ).wrap( "<div class='new'></div>" );
<div class="container"> <div class="new"> <div class="inner">Hello</div> </div> <div class="new"> <div class="inner">Goodbye</div> </div> </div>
<div class="container"> <div class="inner">Hello</div> <div class="inner">Goodbye</div></div>
$( ".inner" ).wrapAll( "<div class='new' />");
<div class="container"> <div class="new"> <div class="inner">Hello</div> <div class="inner">Goodbye</div> </div> </div>
<div class="container"> <div class="inner">Hello</div> <div class="inner">Goodbye</div></div>
$( ".inner" ).wrapInner( "<div class='new'></div>");
<div class="container"> <div class="inner"> <div class="new">Hello</div> </div> <div class="inner"> <div class="new">Goodbye</div> </div> </div>
pTags = $( "p" ).unwrap();
$( "input:checkbox:checked" ).val();
$( "input" ).val( ‘hello’ ); $( "input" ).on( "blur", function() { $( this ).val(function( i, val ) { return val.toUpperCase(); }); });
var title = $( "em" ).attr( "title" );
$( "#greatphoto" ).attr( "alt", "Beijing Brush Seller" ); $( "#greatphoto" ).attr({ alt: "Beijing Brush Seller", title: "photo by Kelly Clark" }); $( "#greatphoto" ).attr( "title", function( i, val ) { return val + " - photo by Kelly Clark"; });
$( elem ).prop( "checked" )
$( "input" ).prop( "checked", true ); $( "input[type='checkbox']" ).prop( "checked", function( i, val ) { return !val; }); $( "input[type='checkbox']" ).prop({ disabled: true });
$( "body" ).data( "foo", 52 ); $( "body" ).data( "bar", { myType: "test", count: 40 } ); $( "body" ).data( { baz: [ 1, 2, 3 ] } );
.data(key) / .data() 获取获取data设置的数据或者HTML5 data-*属性中的数据
alert( $( "body" ).data( "foo" ) ); alert( $( "body" ).data() ); alert( $( "body" ).data( "foo" ) ); // undefined $( "body" ).data( "bar", "foobar" ); alert( $( "body" ).data( "bar" ) ); // foobar
The above is the detailed content of Detailed explanation of the usage of jquery to get elements, wrap elements and insert element attributes. For more information, please follow other related articles on the PHP Chinese website!