動態創建節點
js的方法
var box = document.getElementById("box"); var a = document.createElement("a"); box.appendChild(a); a.setAttribute("href", "http://web.itcast.cn"); a.setAttribute("target", "_blank"); a.innerHTML = "傳智大前端";
jquery的方法
$(function () { $("#box").append('<a href="http://web.itcast.cn" target="_blank">傳智大前端</a>'); });
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> <style> div { width: 400px; height: 400px; background-color: pink; } </style> </head> <body> <!--<a href="http://web.itcast.cn" target="_blank">傳智大前端</a>--> <div id="box"></div> <script src="jquery-1.12.4.js"></script> <script> $(function () { $("#box").append('<a href="http://web.itcast.cn" target="_blank">傳智大前端</a>'); }); </script> </body> </html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/14830.html
標籤:jQuery
上一篇:jQuery---音樂導航
下一篇:jQuery---創建和添加節點
