jquery nodename返回undefined [英] jquery nodename returning undefined

查看:95
本文介绍了jquery nodename返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码不适用于任何特定内容。我只是想成功获取元素的tagName或nodeName。但是,当我运行以下代码时,我总是收到一条警告未定义。我想知道是不是因为这个函数在文档准备好后执行了?我应该做这个不同的地方吗?或者它可能是我的其他JavaScript代码以某种方式冲突(我会怀疑)。

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).

 $(document).ready(function(){
        $('#first').hover(function() {
            alert($('#last').nodeName);
        });
    });


推荐答案

您正在尝试访问非成员 jQuery 对象。
使用这些DOM元素访问器之一来检索这些属性:

You are trying to access a non-member of the jQuery object. Use one of these DOM element accessors to retrieve these properties:

$('#last')。get(0)。 nodeName

OR

$('#are')[0] .nodeName

OR

document.getElementById('last').nodeName

这篇关于jquery nodename返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆