The following is my block of code. since I haven't installed Firebug in IE, Every time when I try to test my code in IE I'm getting an error message console is undefined. so I decided and developed this block of code, so that console.log work only in firefox and to avoid error messages in IE开发者_开发问答.
function clog() {
if(window.console && window.console.firebug) {
var a =[];
for(var i=0;i<arguments.length;i++) {
a.push(arguments[i]);
}
console.log(a.join(' , '));
}
}
my code is working fine and I'm getting the results which I wanted,
but when I tried to use the above code on jQuery ( for example clog($('body')); ),
the result which I expected is to be jQuery(body) . but I'm getting the result as [object Object]
How can I get The results which I expected ?
Thank you !!
When you call a selector like that, say $('body') what you're doing is creating an object, a jQuery object...so your output is correct.
If you want to display something other than it's .toString(), then you should call that property, for example:
$('body').selector //body
$('body').length //1
$('body').context //document
If all you're using is console.log, I find just creating it if it's missing (as opposed to checking whenever you want to use it) is much easier, just have this run before any of your logging code:
if (typeof console == "undefined") console = { log: function () { } };
Then you can remove your current if check.
I always write a wrapper function (to keep non 'console' browers from having problem)
function log(msg) {
try {
console.log(msg);
} catch(e){}
}
You could examine the "msg" object, then check the type to determine whether it's a "jQuery" object, and extract the data.
console.log(a);
instead of
console.log(a.join(' , '));
should do it.
Array.prototype.join will concatenate all array entrys into a String. That means
var b = [{}, "test"];
b.toString()
will evaluate to "[object Object],test" regardless what methods or members are within that object. You just lose that information calling .toString().
加载中,请稍侯......
精彩评论