帶有示例的 jQuery appendTo()
jQuery 中的 appendTo() 方法用於在所選元素的末尾插入 HTML 元素。
語法
語法如下 −
$(content).appendTo(selector)
其中,content 是要插入的內容。
示例
下面讓我們看一個示例來實現 jQuery appendTo() 方法 −
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("<em>New</em>").appendTo("h3"); }); }); </script> </head> <body> <h1>Blog Posts</h1> <h3>How to install Java?</h3> <h3>How to install Ruby?</h3> <button>Click me to check new and old post status</button> </body> </html>
輸出
這會產生以下輸出 −
現在,單擊上面的按鈕 −
廣告