2012-04-02 88 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''(假设这是一个'