实例 使用 prepend() 方法插入内容

x
 
<!DOCTYPE html>
<html>
<head>
<script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
function prependText() {
  var txt1 = "<p>Text.</p>";        // Create text with HTML
  var txt2 = $("<p></p>").text("Text.");  // Create text with jQuery
  var txt3 = document.createElement("p");
  txt3.innerHTML = "Text.";         // Create text with DOM
  $("p").prepend(txt1, txt2, txt3);     // Prepend new elements
}
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button onclick="prependText()">Prepend text</button>
</body>
</html>
                    

输出结果