热门关键字:
jquery > jquery教程 > jquery教程 > 如何在jQuery中向DOM添加新元素

如何在jQuery中向DOM添加新元素

295
作者:管理员
发布时间:2021/2/4 14:05:04
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=3830
答:使用jQueryappend()或prepend()方法
您可以使用jQueryappend()或prepend()方法向DOM添加或插入元素。jQueryappend()方法将内容插入到匹配元素的末尾,而prepend()方法将内容插入到匹配元素的开头。


下面的示例将向您展示如何使用jQuery方法轻松地将新项目添加到HTML有序列表的末尾append()。让我们尝试一下,看看它是如何工作的:


例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Add Elements to DOM</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
    $(document).ready(function(){
        $("button").click(function(){
            $("ol").append("<li>list item</li>"); 
        });
    });
</script>
</head>
<body>
    <button>Add new list item</button>
    <ol>
        <li>list item</li>
        <li>list item</li>
        <li>list item</li>
    </ol>
</body> 
</html>




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:jQuery
友荐云推荐