下面这个属性是什么意思?

use*_*948 1 javascript jquery

我在网上看过这个例子:

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

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

    $question.append($questionText);

    });
Run Code Online (Sandbox Code Playgroud)

它的位置是'.attr('name',$ this.attr('name'))',这是什么意思?这是否与'id'属性#questionTextArea具有相同的'name'属性,或者与'class'属性'textAreaQuestion'具有相同的'name'?

谢谢

Mic*_*ski 6

这是将name每个新创建<textarea>name属性分配给属性#questionTextArea.

// 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');
Run Code Online (Sandbox Code Playgroud)

请注意,因为它是一个查询的id,所以应该只有其中一个,因此.each()循环是不必要的.同样的事情可以通过以下方式完成:

var $questionText = $("<textarea class='textAreaQuestion'></textarea>")
   .attr('name', $('#questionTextArea').attr('name'))
   .attr('value', $('#questionTextArea').val());
Run Code Online (Sandbox Code Playgroud)