2009-09-08 30 views
2

我的jQuery像這樣建立了一個輸入框:jQuery文檔()不工作

val input = $('<input class="pick_date" ... />') 

但輸入的.html()方法不返回$內輸入的字符串。有誰知道爲什麼?

編輯: 啊,我明白這個問題。有沒有辦法獲得整個輸入框的html表示而不僅僅是條目?

+0

發現我的新的答案 – Ropstah 2009-09-10 09:43:54

回答

4

你逝去的<input />這是一個自閉的標籤。

var input = $('<input class="pick_date">Html here</input>'); 
alert(input.html()); 

如果你路過<input>Html here</input>(這是有效的XML,但不是HTML據我所知),你可以用像這樣的html的()函數部分「這裏的Html」檢索除了您編輯的問題:

$('<input />').outerHtml(); 

這應該工作.. :)

本ofcourse(source):

(function($) { 
    $.fn.outerHTML = function() { 
     return $('<div>').append(this.eq(0).clone()).html(); 
    }; 
})(jQuery) 
+0

看到更新回答 – Ropstah 2009-09-08 22:04:38

+2

是不是outerHtml插件(或只有IE)?這不是jQuery的一部分。 – seth 2009-09-08 22:13:21

+0

真的,我忘了粘貼使它成爲jQuery插件的代碼:) – Ropstah 2009-09-09 11:09:20

0

我想,也許你(尋找追加):

$("div#form").append('<input class="pick_date" ... />'); 
+0

可能要額外閱讀:http://stackoverflow.com/questions/268490/jquery-document-createelement-equivalent – pifantastic 2009-09-08 21:59:07

0

就不得不面對這個問題。

如果您使用ASP.NET,這可能是因爲ASP.NET改變ID名稱,如果添加 「RUNAT =」 服務器」

所以不是這樣:

<td id="mytd" runat="server"></td> 
$('#mytd').html() 

試着這樣做:

<td id="mytd" class="myclass" runat="server"></td> 
$('.myclass').html()