爲什麼我的新元素包裝在一個數組中?爲什麼我的新元素包裝在一個數組中?
$('<a>')
[<a></a>]
當我嘗試,我得到一個DOM例外8誤差appendChild
使用它。
編輯:這是一個例子。我的確切代碼是
addendum = $("<a>", {href: download_url, text:"Download .nupkg file"})
badge = $(".nuget-badge")[0]
badge.appendChild(addendum)
爲什麼我的新元素包裝在一個數組中?爲什麼我的新元素包裝在一個數組中?
$('<a>')
[<a></a>]
當我嘗試,我得到一個DOM例外8誤差appendChild
使用它。
編輯:這是一個例子。我的確切代碼是
addendum = $("<a>", {href: download_url, text:"Download .nupkg file"})
badge = $(".nuget-badge")[0]
badge.appendChild(addendum)
所有jQuery對象都是類似數組的對象。選擇[0]
集合中的第一個元素將只返回一個元素。如果您改爲記錄jQuery對象,它將顯示爲控制檯中的數組。
請注意,jQuery有一個名爲.append的內置方法,應該可以做你想做的事情。
$('<a>').append('<span>Hello World!</span>');
或
$('<a>').append(someotherelement);
或您指定代碼:
$(".nuget-badge").append($("<a>", {href: download_url, text:"Download .nupkg file"}))
或(首選)
$("<a>", {href: download_url, text:"Download .nupkg file"}).appendTo(".nuget-badge");
你可以把一個元素body
這樣的:
$('body').append($('<a>',{text:"test", href:"#"}));
所以我看不出的方式一個真正的問題,你所創建的元素
編輯
試試這個:
var addendum = $("<a>", {href: download_url, text:"Download .nupkg file"});
$(".nuget-badge").append(addendum);
你能展示如何使用的appendChild那是行不通的? – Popnoodles 2013-02-20 20:23:50
如果您使用jQuery,爲什麼不使用它的'.append()'方法? – nnnnnn 2013-02-20 20:24:10