2012-04-02 3 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'과 동일한 '이름'속성과 동일한 '이름'속성을 제공합니까?

감사

+0

이, 내가 questionTextArea –

+0

때문에 ID와 Name 속성을 모두 동일하게 'questionTextArea'를 말하고 싶지만 : 같은 일이 같은 것을 달성 할 수 있을까? – user1304948

답변

6

#questionTextAreaname 속성에 각각 새로 <textarea> 생성에 name 속성을 할당한다. 이 쿼리에 id이기 때문에, 거기에 단지 이들 중 하나가 될, 따라서 .each() 루프가 필요의 종류해야

// 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'); 

참고. 당신은 $를 사용하는 것처럼

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

그래서이 이름 속성이 실제로 무엇과 같은지 물어볼 수 있습니까? 즉 'name = ...' – user1304948

+0

@ user1304948 게시 된 코드에서'name'의 실제 값이 무엇인지 알 수 없습니다. 'id ='questionTextArea ''(''이라고 가정)이있는 요소에서'name = 'something'이 무엇이든간에 새로 생성 된 이름은'name = 'something''으로 설정됩니다. –

관련 문제