我在网上看过这个例子:
$('#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'?
谢谢
这是将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)
| 归档时间: |
|
| 查看次数: |
320 次 |
| 最近记录: |