0

我想将从 textarea 获得的字符串值传递给 focus() 和 blur(),但为什么我会得到 [object object]?

我使用 each() 成功获取了每个 textarea 的字符串值,并将字符串存储在 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不会像其他浏览器那样接受它有什么问题!??

4

1 回答 1

1

正如评论者所说,您创建的处理焦点和模糊的函数具有定义为 value_default 的第一个参数(事件参数),它覆盖了您在更高层声明的那个。

因此,如果您这样做,它应该可以工作:

.focus(function(){.blur(function(){

因此,您应该能够访问您之前声明的 value_default,因为它仍在范围内并且不会被您的函数参数覆盖。

于 2010-11-19T16:28:33.390 回答