了解了这些才能开始发挥jQuery的威力

2020-05-23 06:15:57易采站长站整理

  <div class=”new”>
    <div class=”inner”>Hello</div>
    <div class=”inner”>Goodbye</div>
  </div>
</div>


.wrapInner(wrappingElement) / .wrapInner(function(index)) 包裹匹配元素内容,这个不好说,一看例子就懂


Wrap an HTML structure around the content of each element in the set of matched elements.



<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>


.unwap() 把DOM元素的parent移除


Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.


pTags = $( “p” ).unwrap();


属性方法


.val() 获取元素的value值


Get the current value of the first element in the set of matched elements.



$( “input:checkbox:checked” ).val();


.val(value) /.val(function(index,value)) 为元素设置值,index和value同样是指在为集合中每个元素设置的时候该元素的index和原value值


Set the value of each element in the set of matched elements.



$( “input” ).val( ‘hello’ );
$( “input” ).on( “blur”, function() {
  $( this ).val(function( i, val ) {
    return val.toUpperCase();
  });
});


.attr(attributeName) 获取元素特定属性的值


Get the value of an attribute for the first element in the set of matched elements.



var title = $( “em” ).attr( “title” );


.attr(attributeName,value) / .attr(attributesJson) / .attr( attributeName, function(index, attr) ) 为元素属性赋值


Set one or more attributes for the set of matched elements.



$( “#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”;
});