prepend

英 [pri:'pend]? ?美 [pri:'pend]??

vt.預(yù)先考慮,預(yù)先計(jì)劃,預(yù)謀

jquery prepend()方法 語法

作用:prepend() 方法在被選元素的開頭(仍位于內(nèi)部)插入指定內(nèi)容。prepend() 和?prependTo()?方法作用相同。差異在于語法:內(nèi)容和選擇器的位置,以及 prependTo() 無法使用函數(shù)來插入內(nèi)容。

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

參數(shù):

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

使用函數(shù)來附加內(nèi)容:使用函數(shù)在被選元素的開頭插入指定的內(nèi)容。

語法:$(selector).prepend(function(index,html))

參數(shù):

參數(shù)描述
function(index,html)?必需。規(guī)定返回待插入內(nèi)容的函數(shù)。
index可選。接受選擇器的 index 位置。
html?可選。接受選擇器的當(dāng)前 HTML。

jquery prepend()方法 示例

<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(){
  $("button").click(function(){
    $("p").prepend("<b>Hello world!</b> ");
  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>在每個(gè) p 元素的開頭插入內(nèi)容</button>
</body>
</html>
運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例