How to extend jQuery to make it easier to retrieve the tagName

前端 未结 4 495
不思量自难忘°
不思量自难忘° 2020-12-06 09:32

I am looking to extend jQuery so I can easily retrieve the tagName of the first element in a jQuery object. This is what I have come up with, but it doesn\'t seem to work:

4条回答
  •  独厮守ぢ
    2020-12-06 10:33

    You may wish to add a toLowerCase() to make it more consistent (and XHTML compliant).

    $.fn.tagName = function() {
        return this.get(0).tagName.toLowerCase();
    }
    
    alert($('#testElement').tagName());
    

提交回复
热议问题