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


$( ".inner" ).before( "<p>Test</p>" );
$( ".container" ).before( $( "h2" ) );
$( "p" ).first().before( newdiv1, [ newdiv2, existingdiv1 ] );
$( "p" ).before( "<b>Hello</b>" );
$( "p" ).before( document.createTextNode( "Hello" ) );

.insertBefore(target) 把对象插入到target之前(同样不是头部,是同级)

Insert every element in the set of matched elements before the target.

复制代码 代码如下:

$( "h2" ).insertBefore( $( ".container" ) );

.after([content][,content]) / .after(function(index)) 和before相反,在对象后面(不是尾部,而是外面,和对象并列同级)插入内容,参数和append类似

Insert content, specified by the parameter, after each element in the set of matched elements.

复制代码 代码如下:


$( ".inner" ).after( "<p>Test</p>" );
$( "p" ).after( document.createTextNode( "Hello" ) );

.insertAfter(target) 和insertBefore相反,把对象插入到target之后(同样不是尾部,是同级)

Insert every element in the set of matched elements after the target.

复制代码 代码如下:


$( "<p>Test</p>" ).insertAfter( ".inner" );
$( "p" ).insertAfter( "#foo" );

包裹元素

.wrap(wrappingElement) / .wrap(function(index)) 为每个对象包裹一层HTML结构,可以是selector, element, HTML string, jQuery object

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

复制代码 代码如下:


<div>
  <div>Hello</div>
  <div>Goodbye</div>
</div>$( ".inner" ).wrap( "<div></div>" );
<div>
  <div>
    <div>Hello</div>
  </div>
  <div>
    <div>Goodbye</div>
  </div>
</div>

.wrapAll(wrappingElement) 把所有匹配对象包裹在同一个HTML结构中

Wrap an HTML structure around all elements in the set of matched elements.

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdzdys.html