<html>
<body>
<script type="text/javascript" src="jquery.js"></script><noscript></noscript>
<form>
<input type="text" /><br />
<input type="text" /><br />
<input type="button" id="login" />
</form>
<script type="text/javascript">
var login = $("#login");
//var login=document.getElementById('login');
alert(login.id);
</script>
</body>
</html>
alert出的是undefine   why?用注释的那个就可以啊

解决方案 »

  1.   

    login[0].id这样可以得出,var login = $("#login")返回的是
    [0]: {object}
        context: {object}
        jquery: "1.3.2"
        length: 1
        selector: "#login"
    你所需要的id是在数组中了。debug一下就知道了
      

  2.   

    Thanks for your help!   
    看了 jquery 官方的 tutorials也没看到return是array的!
    (http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_select_an_item_using_class_or_id.3F)   郁闷
      

  3.   

    Matches a single element with the given id attribute.
    明明写single,但还是返回array   不知道为什么  
      

  4.   

    汗...这个写的是作用,匹配给定ID的一个元素,jquery封装的对象返回的是什么,你看源码就很清楚
      

  5.   

    返回的是个jquery对象,不是数组,由于id有唯一性,所以官方说匹配一个唯一的元素,
      

  6.   

    #id Returns: Array<Element>
    Matches a single element with the given id attribute.在http://docs.jquery.com/Selectors 看到的  开始奇怪中了 - -!
      

  7.   

    不知道是不是为了通用....没时间研究jQuery的源码....猜想这个选择器的选择方法是不是写成id,class,tagName都调用的一个方法,为了通用所以返回数组只是猜想
      

  8.   

    我也是刚刚看 jquery  一时间转不过这个弯啊!    - -!
      

  9.   

    你得到的不是一个html对象,而是一个jquery对象,jquery对象是没有Id属性的,用attr('id')应该比较靠谱.
      

  10.   


            // alert(elemArr[i].id);     //可以
             alert(elemArr[i].attr("id")); //不行?