jquery nodename returning undefined

余生颓废 提交于 2019-11-29 15:59:17

问题


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);
        });
    });

回答1:


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

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

OR

document.getElementById( 'last' ).nodeName




回答2:


Use the prop() of jQuery:

alert($('#last').prop("nodeName"));


来源:https://stackoverflow.com/questions/2770207/jquery-nodename-returning-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!