Javascript 为什么对象的控制台日志输出字符串?
以下代码选择contenteditable中光标所在的当前节点:Javascript 为什么对象的控制台日志输出字符串?,javascript,contenteditable,Javascript,Contenteditable,以下代码选择contenteditable中光标所在的当前节点: var selection; if (window.getSelection){ selection = window.getSelection(); } else if (document.selection && document.selection.type !== "Control") { selection = document.selection; } var a
var selection;
if (window.getSelection){
selection = window.getSelection();
}
else if (document.selection && document.selection.type !== "Control") {
selection = document.selection;
}
var anchor_node = selection.anchorNode; //current node on which cursor is positioned
console.log(anchor_node);
console.log显示“text”,即节点的内容;但anchor_节点实际上是一个对象
$('.result').val(anchor_node);
输出“[对象]”;它的工作原理是:
$('.result').val(anchor_node.data);
那么为什么日志只输出内容字符串呢?如何记录整个anchor\u node对象?您没有提到正在使用的浏览器/调试器,但是Chrome的开发工具将通过呈现外部HTML来输出元素,但它是可实时单击的;如果需要,您甚至可以右键单击它并选择“在元素面板中显示” 另一种选择(因为nikhil删除了他的答案,不知道为什么,去投票了,结果它不在了)是使用
console.dir
,它不试图特别处理DOM元素
Reconsole.log
对DOM元素的特殊处理(在某些开发工具中):
例如:
console.log(document.querySelector(“a”)代码>
不确定nikhil删除的原因,但您应该使用console.dir
。具体来说,console.log对DOM元素进行了特殊处理,而console.dir则没有。在尝试查看DOM JS对象的完整表示时,这通常很有用。
ok,谢谢