2

I used $(this) in a function like this $(document).on("dblclick", "td.edit", function(){ makeEditable($(this)); }); to obtain the html element info that is triggered. But when I do either console.log or alert, the result will shows only [object Object]. I wonder is there methods I can use to see what are actually inside? Thank You

After Tibos's suggested methods I get this msg in chrome console:

[Object, jquery: "1.10.2", constructor: function, init: function, selector: "", toArray: function…]
0: Object
length: 1
__proto__: Object[0]
4

2 回答 2

4
var foo = { bar : 1 };

当你这样做时:

console.log(foo.toString())

它将不再显示对象,而是显示对象的字符串表示形式,与大多数对象一样是 [object Object]。如果您隐式转换它,也会发生同样的情况:

console.log('This is my object: ' + foo); // This is my object [object Object]

要显示对象而不是其字符串表示,您需要将对象作为参数传递给 console.log:

console.log(foo); // { bar : 1 }
console.log('This is my object', foo); // This is my object { bar : 1 }

在您的示例中,如果您想在触发 dblclick 事件的 HTML 元素周围显示 jQuery 包装器对象,您可以执行以下操作:

$(document).on("dblclick", "td.edit", function(){ console.log($(this)); });

如果你只想要 HTML 元素,你可以这样做:

$(document).on("dblclick", "td.edit", function(){ console.log(this); });
于 2013-12-11T14:26:10.190 回答
0

尝试使用

$(this).prop('outerHTML');

或者

$(this)[0].outerHTML
于 2013-12-11T14:16:45.710 回答