简体   繁体   中英

What does this attribute below mean?

I have seen this example on the web:

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

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

    $question.append($questionText);

    });

Where it says '.attr('name',$this.attr('name'))', what does that mean? Does that give the same 'name' attribute as the 'id' attribute #questionTextArea or the same 'name' as the 'class' attribute 'textAreaQuestion'?

Thanks

This is assigning the name attribute on each newly created <textarea> to the name attribute of #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');

Note that since it's an id queried, there should only be one of these, and therefore the .each() loop is kind of unnecessary. The same thing could be accomplished with something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM