Check if element is a div

前端 未结 11 1355
南方客
南方客 2021-02-02 04:44

How do I check if $(this) is a div, ul or blockquote?

For example:

if ($(this) is a div) {
  alert(\'         


        
11条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 05:45

    Some of these solutions are going a bit overboard. All you need is tagName from regular old JavaScript. You don't really get any benefit from re-wrapping the whole thing in jQuery again, and especially running some of the more powerful functions in the library to check the tag name. If you want to test it on this page, here's an example.

    $("body > *").each(function() {
      if (this.tagName === "DIV") {
        alert("Yeah, this is a div");
      } else {
        alert("Bummer, this isn't");
      }
    });
    

提交回复
热议问题