2011-10-16 24 views
0
$("#write").click(function(){ 
    $("#msg").$('#quote').html(); 

}); 

我正在學習jquery(js也是新的)。這個函數寫入一個文本框id = msg。不過,我在抽取內容時遇到問題。不知道如何在jQuery中使用變量

如何拉取div的內容並在我的函數(#quote)中使用?現在它只是寫#quote,而不是名爲quote的div的內容。

也試過:

var quote = $("#quote").html(); 
    $("#msg").$(quote.val).html(); 

的想法。

回答

3

就像你寫的,但第二行應該是$("#msg").html(quote);

#quote的內容已經在quote中,並且爲了用它替換#msg的內容,您將它作爲字符串傳遞給.html()

更多.html()http://api.jquery.com/html/

+0

+1有更多解釋,並且快9秒! – JCOC611

+0

非常感謝,我非常接近。所以創建一個新的變量是我猜測的唯一方法嗎? – domino

+0

@Dvir:+1打我9秒;)@domino:你不必創建一個新的變量。您可以使用:'$('#msg')。html($('#quote')。html());' –

1

你有一個語法錯誤。試試這個:

$("#write").click(function(){ 
    var quote = $('#quote').html(); 
    $("#msg").html(quote); 
}); 
0

好,我認爲你應該做

$("#msg").val($("#myDiv").html()); 

這將設置DIV的文本框的值的值。

相關問題