How can I determine the element type of a matched element in jQuery?

前端 未结 9 1331
鱼传尺愫
鱼传尺愫 2020-12-01 03:13

I\'m matching ASP.Net generated elements by ID name, but I have some elements which may render as text boxes or labels depending on the page context. I need to figure out wh

相关标签:
9条回答
  • 2020-12-01 04:08

    tagName what a nice tip. I would like to suggest also to use tagName.toLowerCase() since the value returned depends on the document type (HTML or XML/XHTML).

    See: http://reference.sitepoint.com/javascript/Element/tagName

    0 讨论(0)
  • 2020-12-01 04:10

    Consider this solution without using each():

    var elements = $("[id$=" + endOfIdToMatch + "]");
    var vals = elements.is("input").val();
    var htmls = elements.is("label").html();
    var contents = vals.concat(htmls);
    

    Have a look at the documentation for is.

    0 讨论(0)
  • 2020-12-01 04:11

    This the best way to Get the element type

    function tgetelementType( elmentid )
    {
    
        var TypeName = $('#' + elmentid).get(0).tagName;
        var TypeName2 = $('#' + elmentid).get(0).type;
    
    
        if($('#' + elmentid).get(0).tagName== "INPUT")
        {
           return $('#' + elmentid).get(0).type.toUpperCase()
        }
        else 
        {
            return $('#' + elmentid).get(0).tagName.toUpperCase() ; 
        }
    }
    
    0 讨论(0)
提交回复
热议问题