html jquery向body追加元素到开头
要向<body>元素的开头添加元素,可以使用prepend()方法。以下是一个示例:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
<div class="container">
<p>这是一个段落。</p>
</div>
<script>
$(document).ready(function() {
var newElement = $("<h1>这是一个新的标题</h1>");
$("body").prepend(newElement);
});
</script>
</body>
</html>
在上述示例中,我们使用prepend()方法将一个新的<h1>元素添加到<body>元素的开头。
原文地址: http://www.cveoy.top/t/topic/iWgT 著作权归作者所有。请勿转载和采集!