before

英 [b??f?:(r)]? ?美 [b??f?r,-?for]??

prep.在…之前;先于,優(yōu)于;當著…的面;與其…

conj.在…之前;在…以前;比…早些;與其…

adv.先前,從前;在前,在前方

jquery before() 方法 語法

作用:before() 方法在被選元素前插入指定的內(nèi)容。

語法:$(selector).before(content)

參數(shù):

參數(shù)描述
content必需。規(guī)定要插入的內(nèi)容(可包含 HTML 標簽)。

使用函數(shù)來插入內(nèi)容:使用函數(shù)在指定的元素前面插入內(nèi)容。

語法:$(selector).before(function(index))

參數(shù):

參數(shù)描述
function(index)?必需。規(guī)定返回待插入內(nèi)容的函數(shù)。
index?可選。接收選擇器的 index 位置。

jquery before() 方法 示例

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
    $("p").before("<p>Hello world!</p>");
  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button class="btn1">在每個段落前面插入新的段落</button>
</body>
</html>
運行實例 ?

點擊 "運行實例" 按鈕查看在線實例