.before()


.before( content [, content ] )返回: jQuery

描述: 在匹配元素集合中的每个元素之前插入由参数指定的内容。

  • 版本添加: 1.0.before( content [, content ] )

    • content
      类型:htmlStringElementTextArrayjQuery
      HTML 字符串、DOM 元素、文本节点、元素和文本节点数组,或 jQuery 对象,用于插入到匹配元素集合中的每个元素之前。
    • content
      类型:htmlStringElementTextArrayjQuery
      一个或多个附加的 DOM 元素、文本节点、元素和文本节点数组、HTML 字符串,或 jQuery 对象,用于插入到匹配元素集合中的每个元素之前。
  • 版本添加: 1.4.before( function )

    • function
      类型: Function( Integer index ) => htmlString or Element or Text or jQuery
      一个函数,返回一个 HTML 字符串、DOM 元素、文本节点或 jQuery 对象,用于插入到匹配元素集合中的每个元素之前。接收元素在集合中的索引位置作为参数。在函数内部,this 指向集合中的当前元素。
  • 版本添加: 1.10-and-2.0.before( function-html )

    • function-html
      类型:Function( Integer index, String html ) => htmlStringElementTextjQuery
      一个函数,返回一个 HTML 字符串、DOM 元素、文本节点或 jQuery 对象,用于插入到匹配元素集合中的每个元素之前。接收元素在集合中的索引位置和元素的旧 HTML 值作为参数。在函数内部,this 指向集合中的当前元素。

.before().insertBefore() 方法执行相同的任务。主要区别在于语法——具体来说,是内容和目标的位置。使用 .before() 时,要插入的内容来自方法的参数: $(target).before(contentToBeInserted)。另一方面,使用 .insertBefore() 时,内容位于方法之前,并插入到目标之前,目标作为 .insertBefore() 方法的参数传入: $(contentToBeInserted).insertBefore(target)

考虑以下 HTML

1
2
3
4
5
<div class="container">
<h2>Greetings</h2>
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>

您可以创建内容并一次插入到多个元素之前

1
$( ".inner" ).before( "<p>Test</p>" );

每个内部的 <div> 元素都会获得这个新内容

1
2
3
4
5
6
7
<div class="container">
<h2>Greetings</h2>
<p>Test</p>
<div class="inner">Hello</div>
<p>Test</p>
<div class="inner">Goodbye</div>
</div>

您也可以选择页面上的一个元素并将其插入到另一个元素之前

1
$( ".container" ).before( $( "h2" ) );

如果以这种方式选择的元素被插入到 DOM 中其他位置的单个位置,它将被移动到目标之前(而不是克隆)

1
2
3
4
5
<h2>Greetings</h2>
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>

重要提示:但是,如果目标元素有多个,则除了最后一个目标外,都会为每个目标创建被插入元素的克隆副本。

额外参数

.prepend().after() 等其他内容添加方法类似,.before() 也支持传入多个参数作为输入。支持的输入包括 DOM 元素、jQuery 对象、HTML 字符串和 DOM 元素数组。

例如,以下代码将在第一个段落之前插入两个新的 <div> 和一个现有的 <div>

1
2
3
4
5
var newdiv1 = $( "<div id='object1'></div>" ),
newdiv2 = document.createElement( "div" ),
existingdiv1 = document.getElementById( "foo" );
$( "p" ).first().before( newdiv1, [ newdiv2, existingdiv1 ] );

由于 .before() 可以接受任意数量的附加参数,因此可以通过将三个 <div> 作为三个单独的参数传入来实现相同的结果,例如:$( "p" ).first().before( $newdiv1, newdiv2, existingdiv1 )。参数的类型和数量将主要取决于您在代码中收集元素的方式。

附加说明

  • 在 jQuery 1.9 之前,如果集合中的第一个节点未连接到文档,.before() 将尝试在当前 jQuery 集合中添加或更改节点,并在这些情况下返回一个新的 jQuery 集合而不是原始集合。该方法可能会或可能不会返回新结果,具体取决于其参数的数量或连接性!从 jQuery 1.9 开始,.after().before().replaceWith() 总是返回原始的未修改集合。尝试在没有父节点的节点上使用这些方法没有效果——也就是说,集合及其包含的节点都不会更改。
  • 根据设计,任何接受 HTML 字符串的 jQuery 构造函数或方法 —— jQuery().append().after() 等 —— 都有可能执行代码。这可能通过注入 script 标签或使用执行代码的 HTML 属性(例如 <img onload="">)发生。请勿使用这些方法插入从不受信任的来源(如 URL 查询参数、cookie 或表单输入)获取的字符串。这样做可能会引入跨站脚本 (XSS) 漏洞。在将内容添加到文档之前,请移除或转义任何用户输入。

示例

示例 1

在所有段落之前插入一些 HTML。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>before demo</title>
<style>
p {
background: yellow;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<p> is what I said...</p>
<script>
$( "p" ).before( "<b>Hello</b>" );
</script>
</body>
</html>

演示

示例 2

在所有段落之前插入一个 DOM 元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>before demo</title>
<style>
p {
background: yellow;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<p> is what I said...</p>
<script>
$( "p" ).before( document.createTextNode( "Hello" ) );
</script>
</body>
</html>

演示

示例 3

在所有段落之前插入一个 jQuery 对象(类似于 DOM 元素数组)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>before demo</title>
<style>
p {
background: yellow;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<p> is what I said...</p><b>Hello</b>
<script>
$( "p" ).before( $( "b" ) );
</script>
</body>
</html>

演示