在jquery中追加一个元素

时间:2014-09-17 06:47:36

标签: javascript jquery

var main_div = $('div[class^="select"]');  
var download_button = document.createElement('a');

download_button.setAttribute("id", "download");
download_button.style.color = "blue";
download_button.href = "#";
download_button.innerHTML = "Download";

main_div.append(download_button);

Nothing Happens,没有附加新元素来选择这个错误吗?

3 个答案:

答案 0 :(得分:1)

使用

main_div.append(download_button);

而不是

main_div.appendTo(download_button);

更新 -

DEMO

答案 1 :(得分:0)

你可以在jQuery中这样做:

 var main_div = $('div[class^="select"]'); 

 $("<a/>")
   .attr("id","download")
   .css("color","blue")
   .attr("href","#")
   .html("Download")
   .appendTo(main_div);

答案 2 :(得分:0)

不要像你那样混合使用jQuery和JS ......

var main_div = $('div.select');  
var download_button = $('<a id="download" style="color:blue;" href="#">Download</a>');

main_div.append(download_button);