2010-11-19 10 views
0

textarea에서 얻은 문자열 값을 focus() 및 blur(), 으로 전달하려고하지만 왜 [object object]를 얻을 것입니까? 매개 변수 전달 : 함수에 문자열 값 전달

나는 각 (성공적으로 각 텍스트 영역의 문자열 값을 얻을)와 나는 VAR에 문자열을 저장 - 그럼 var value_default = $this.val();

내가이 var$this.focus(function (value_default) {..})에 전달 - 내가 잘못 전달 무엇입니까?

내가이 value_default에 알리면 [object oject]가 표시됩니다. - alert (value_default); 당신이 그것을 볼 필요가있는 경우

다음은 ...

$(".autohide").each(function(){ 

    /* set the variable and store its value */ 
    var $this = $(this); 

    var value_default = $this.val(); 

    var parent_autohide = $this.parents('form'); 

    var parent_button = $('input[type=submit]',parent_autohide).parents("div:.item-form").hide(); 


    var parent_marginbottom = parseInt($this.parents("div:.item-form").css("marginBottom")); 

    $this.parents("div:.item-form").css({margin:'0px'}); 

    alert(value_default); // I will get the string value of each textarea element 

    $this.focus(function (value_default) { 

     alert(value_default); // I will get [object object] 

     var $this = $(this); 

     $this.elastic(); 

     $this.parents('div:.item-form').css({margin:'0px 0px '+parent_marginbottom+'px 0px'}); 

     var $this_parent = $this.parents('form'); 

     var $this_button = $('input[type=submit]',$this_parent).parents("div:.item-form").show(); 


    }).blur(function(value_default){ 

     alert(value_default); // I will get [object object] 

     var $this = $(this); 

     var value_current = $this.val(); 

     if (value_current == value_default) 
     { 
      $this.css({ height: 'inherit'}); 

      $this.parents('div:.item-form').css({margin:'0px'}); 

      var $this_parent = $this.parents('form'); 

      var $this_button = $('input[type=submit]',$this_parent).parents("div:.item-form").hide(); 

     } 
    }); 

}); 

많은 덕분에 전체 코드입니다.

편집 :!

내가 IE에서 오류의 원인이 있었는지 -

$this.css({height: 'inherit'}); // this line will create error on IE 

$this.css({height:''}); // IE likes this 

그래서 IE는 다른 브라우저처럼하지 것이다 '상속'에 어떤 문제가 있는지?

+0

이 경우'$ this.focus (function (value_default) {..})'실제로 함수에'value_default'를 전달하지 않습니다. 'value_default'가 첫 번째 인수 인 함수를 정의하고 있습니다 (이 경우 포커스 이벤트에서 전송 된 이벤트 객체가됩니다) – climbage

답변

1

코멘트 작성자가 말했듯이 포커스와 흐림을 처리하는 함수는 value_default로 정의 된 첫 번째 매개 변수 (이벤트 매개 변수)를 가지고 있습니다.이 매개 변수는 더 높게 선언 한 매개 변수를 재정의합니다.

.focus(function(){ 및 따라서 .blur(function(){

당신이 범위 내에서 여전히 아니라 당신이 이전에 선언 value_default에 액세스 할 수 있어야이 함수의 매개 변수에 의해 무시되고 :

은 그래서 당신은 그냥 할 경우 작동합니다 .

+0

예 IE7을 수용하는 모든 브라우저에서 작동합니다 .... – laukok

+0

경고 표시 내용 지금 IE7에서? –

+0

이것은 나에게 위험한 것처럼 보입니다. 모든 blur/focus 이벤트에 대한'value_default'의 값은 마지막으로 보이는 값이 될 것입니다. http://jsfiddle.net/CcdWt/ - 세 div를 모두 클릭하면 "test3"값을 얻습니다. (잘못 설정하면 문제가 해결됩니다) – climbage