2012-04-02 43 views
1

我看到這個例子網站:此屬性的下列含義是什麼?

$('#questionTextArea').each(function() { 

    var $this = $(this); 
    var $questionText = $("<textarea class='textAreaQuestion'></textarea>") 
        .attr('name',$this.attr('name')) 
        .attr('value',$this.val()); 

    $question.append($questionText); 

    }); 

凡說 '.attr( '名',$ this.attr( '名'))',這是什麼意思?這是否與'id'屬性#questionTextArea或'class'屬性'textAreaQuestion'具有相同的'name'屬性?

感謝

+0

此,我想說questionTextArea – 2012-04-02 00:37:39

+0

所以id和和name屬性都等於「questionTextArea」: 同樣的事情也可能有類似的東西來完成? – user1304948 2012-04-02 00:46:58

回答

6

這是分配在每個新創建的<textarea>屬性name#questionTextAreaname屬性。

// Points $this to the current node the .each() is iterating on (#questionTextArea) 
var $this = $(this); 

// The name attribute of the current node .each() is iterating on 
$this.attr('name'); 

注意,因爲它是查詢一個id,應該只有其中之一,因此.each()循環是一種不必要的。當你正在使用$

var $questionText = $("<textarea class='textAreaQuestion'></textarea>") 
    .attr('name', $('#questionTextArea').attr('name')) 
    .attr('value', $('#questionTextArea').val()); 
+0

所以我可以問這個名稱屬性實際上等於什麼?換句話說'name = ...' – user1304948 2012-04-02 00:43:37

+0

@ user1304948我們不知道從發佈的代碼中得知'name'的實際值是什麼。不管'id ='questionTextArea''(假設這是一個'