参数传递:将字符串值传递给函数
我想将从文本区域获得的字符串值传递给 focus() 和 Blur(), 但为什么我会得到[object object]?
我使用 every() 成功获取每个文本区域的字符串值,并将该字符串存储在 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 不会像其他浏览器一样接受它!?
I want to pass the string value which I get from the textarea into the focus() and blur(),
but why what I will get is [object object]?
I get the string value of each textarea successfully with each() and I store the string in a var - var value_default = $this.val();
Then I pass this var
into $this.focus(function (value_default) {..})
- am I passing it incorrectly?
If I alert this value_default
I will get [object oject] - alert(value_default);
Below is the entire code if you need to see it...
$(".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();
}
});
});
many thanks.
edit:
I know what was causing the error on IE -
$this.css({height: 'inherit'}); // this line will create error on IE
$this.css({height:''}); // IE likes this
so what is wrong with 'inherit' that IE won't take it like other browsers!??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论者所说,您创建的处理焦点和模糊的函数将第一个参数(事件参数)定义为 value_default,它会覆盖您在上面声明的参数。
因此,如果您只执行以下操作,它应该可以工作:
.focus(function(){
和.blur(function(){
)因此您应该能够访问之前声明的 value_default因为它仍在范围内并且没有被函数参数覆盖。
As the commenter has said, the function you create that handles the focus and the blur has the first parameter (event parameter) defined as value_default, which is overriding the one you declare higher up.
So it should work if you just do:
.focus(function(){
and.blur(function(){
Thus you should be able to access the value_default you declare before as it is still within scope and not being overridden by your function parameters.