DIV CSS 佈局教程網

jQuery after() 方法:在被選元素後方插入內容
編輯:JQuery常見問題     

方法定位

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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved