
prepend()方法将指定的内容插入每个选定元素的开头(作为第一个子元素)。
要在所选元素的末尾插入内容,请使用append()方法。
语法:
插入前置内容:
$(selector).prepend(content)
使用函数添加内容:
$(selector).prepend(function(index, html))
实例
在所有段落之前添加一些文本内容:
$("button").click(function(){
$("p").prepend("Hello world");
});测试看看‹/›在所有段落前添加一些HTML:
$("button").click(function(){
$("p").prepend("<b>Hello world</b>");
});测试看看‹/›本示例使用document.createTextNode()创建文本节点并将其添加到所有<p>元素之前:
$("button").click(function(){
$("p").prepend(document.createTextNode("Hello world"));
});测试看看‹/›使用函数添加内容:
$("button").click(function(){
$("p").prepend(function(i){
return "<b>This p element has index " + i + "</b>";
});
});测试看看‹/›参数值
| 参数 | 描述 |
|---|---|
| content | 指定在每个选定元素的开头插入的内容(可以包含HTML标记) 可能的值:
|
| function(index, html) | 指定一个函数,该函数返回要插入的内容
|