after() 的特點是「插在外面」:它把內容放到被選元素的後面,成為兄弟節點,而不是像 append() 那樣塞進元素內部。
效果體驗:https:///keleyi/phtml/jquery/13.htm
$(selector).after(content)
參數 content 為必需,代表要插入的內容,可包含 HTML 標籤。
$(selector).after(function(index))
參數 function(index) 為必需,規定返回待插入內容的函數;其中 index(可選)用來接收選擇器的 index 位置。這種寫法很適合批量生成帶序號的說明文字。
<html>
<head>
<script type="text/javascript" src="https:///keleyi/pmedia/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").after(function(n){
return "<p>The p element above has index " + n + "</p>";
});
});
});
</script>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>在每個 p 元素後插入內容</button>
</body>
</html>
更多參考:https:///a/bjac/2oncd079.htm