Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组中的字符串在jQuery.each()之后不再是字符串_Javascript_Jquery_Firebug - Fatal编程技术网

Javascript 数组中的字符串在jQuery.each()之后不再是字符串

Javascript 数组中的字符串在jQuery.each()之后不再是字符串,javascript,jquery,firebug,Javascript,Jquery,Firebug,当我通过jQuery.each()方法循环字符串数组时,我对字符串数组的行为感到非常困惑。显然,字符串在回调函数中成为jQuery对象。但是,我不能使用this.get()方法来获取原始字符串;这样做会触发一条this.get不是函数的错误消息。我想原因是它不是DOM节点。我可以做$(this).get(),但它使我的字符串成为一个数组(从“foo”到[“f”、“o”、“o”]) 我怎样才能把它抛回弦呢?我需要得到一个String类型的变量,因为我将它传递给其他函数,这些函数比较它们之间的值 我

当我通过
jQuery.each()
方法循环字符串数组时,我对字符串数组的行为感到非常困惑。显然,字符串在回调函数中成为jQuery对象。但是,我不能使用
this.get()
方法来获取原始字符串;这样做会触发一条this.get不是函数的错误消息。我想原因是它不是DOM节点。我可以做
$(this).get()
,但它使我的字符串成为一个数组(从
“foo”
[“f”、“o”、“o”]

我怎样才能把它抛回弦呢?我需要得到一个
String
类型的变量,因为我将它传递给其他函数,这些函数比较它们之间的值

我附上一个独立的测试用例(需要Firebug的控制台):


这适用于我的Firefox 3.5:

$(function(){
    var foo = [];

    foo.push("abc");
    foo.push("def");

    $.each(foo, function(i){
        console.log(typeof ("" + this)); 
        });
});
控制台显示

string string 一串
string编辑/澄清:calllback函数接受两个参数,第二个参数是变量的值。使用它而不是
我已经测试了这两种变体,并且一切都像预期的那样工作(字符串仍然是字符串)


六羟甲基三聚氰胺六甲醚。。。你给了我一个想法:
String(this)
@lvaro G.代理:听起来不错:)我几乎是一个JavaScript noob,所以我倾向于使用第一个可行的hack
this
在字符串中循环时不是jQuery对象,它将字符串包装在
String
对象中。如果您执行
this.toString()
操作,您将获得一个常规字符串。我刚刚遇到了相同的问题,但发现在严格模式下,该操作与预期一样有效。 string string
>>> $.each(['foo','bar'], function(i, s){ console.info( s, typeof s ) })
foo string
bar string

>>> $(['foo','bar']).each(function(i, s){ console.info( s, typeof s ) })
foo string
bar string