定义和用法
after() 方法在被选元素后插入指定的内容。
语法
$(selector).after(content)
参数 描述
content 必需。规定要插入的内容(可包含 html 标签)。
使用函数来插入内容
使用函数在被选元素之后插入指定的内容。
语法
$(selector).after(function(index))
参数 描述
function(index)
必需。规定返回待插入内容的函数。
index - 可选。接收选择器的 index 位置。
法一:
在每个p元素后插入内容:
代码如下:
$("button").click(function(){
$("p").after("<p>hello world!</p>");
});
法二:
代码如下:
$("button").click(function(){
$("p").after(function(n){
return
"<p>the p element above has index " + n + "</p>";
});
});
after中函数必须返回一个html字符串。
以上就是解析jquery中after的两种用法的详细内容。