jQuery - adding elements

Add new HTML content

  • append() - insert content at the end of the selected element
  • prepend() - insert content at the beginning of the selected element
  • after() - insert content after the selected element
  • before() - insert content before the selected element

jQuery append() method

The jQuery append() method inserts content at the end of the selected element (still inside the element).

$(document).ready(function(){
  $("#btn1").click(function(){
    $("p").append(" <b>追加文本</b>。");
  });

  $("#btn2").click(function(){
    $("ol").append("<li>Append list item</li>");
  });
});
</script>
</head>

<body>
<p>This is a paragraph. </p>
<p>This is another paragraph. </p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ol>
<button id="btn1">Add text</button>
<button id="btn2">Add list item</button>
</body>



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325998465&siteId=291194637