Javascript jquery节点名返回未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2770207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery nodename returning undefined
提问by Matthew
This code isn't for anything in particular. I'm just trying to successfully get the tagName or nodeName of an element. However, when I run the following code, I always get an alert saying "undefined". I'm wondering if it's because this function executes when the document is ready? Is there a different place I should be doing this? Or is it probably my other javascript code conflicting somehow (I would doubt).
此代码不适用于任何特定内容。我只是想成功获取元素的 tagName 或 nodeName 。但是,当我运行以下代码时,我总是收到一条警告,说“未定义”。我想知道是不是因为这个函数在文档准备好时执行?我应该在其他地方做这件事吗?或者它可能是我的其他 javascript 代码以某种方式冲突(我会怀疑)。
$(document).ready(function(){
$('#first').hover(function() {
alert($('#last').nodeName);
});
});
回答by steven
Use the prop()of jQuery:
使用prop()jQuery:
alert($('#last').prop("nodeName"));
回答by Jacob Relkin
You are trying to access a non-member of the jQueryobject.
Use one of these DOM element accessors to retrieve these properties:
您正在尝试访问jQuery对象的非成员。使用这些 DOM 元素访问器之一来检索这些属性:
$( '#last' ).get(0).nodeName
$( '#last' ).get(0).nodeName
OR
或者
$( '#last' )[0].nodeName
$( '#last' )[0].nodeName
OR
或者
document.getElementById( 'last' ).nodeName
document.getElementById( 'last' ).nodeName

