English 中文(简体)
如果是零件,则检查
原标题:Check if element is a div

如何核对<代码>$/code>?

例如:

if ($(this) is a div) {
  alert( its a div! );
} else {
  alert( its not a div! some other stuff );
}
最佳回答

Something like this:

if(this.tagName ==  DIV ) {
    alert("It s a div!");
} else {
    alert("It s not a div! [some other stuff]");
}
问题回答

Solutions without jQuery are already posted, so I ll post solution using jQuery

$(this).is("div,ul,blockquote")

To check if this element is DIV

if (this instanceof HTMLDivElement) {
   alert( this is a div );
}

Same for HTMLUListElement for UL,
HTMLQuoteElement for blockquote

如无 j,请见this.tagName=DIV

Keep in mind that the N in tagName is uppercase.

Or, with more tags:

/DIV|UL|BLOCKQUOTE/.test(this.tagName)

if(this.tagName.toLowerCase() == "div"){
    //it s a div
} else {
    //it s not a div
}

编辑:虽然我写了字,但作了许多答复,对翻一番表示担忧。

旧问题,但既然答复没有提及这一点,那么一个没有分类的现代替代方案只能使用一个中央支助事务的挑选人和

element.matches ( div, ul,/2005/4quote );

http://api.jquery.com/is"rel=“nofollow”>。

如果至少其中一部分内容与特定论点相符,则核对目前针对选定人、要素或 j子的整套内容,并报到。

其中一些解决办法正在突破。 你们所需要的是:从老的Java正规版本中提取的tagName。 你们不会真正从重整 thing金中重新获得任何好处,特别是在图书馆管理一些更强大的功能,以检查标签。 如果你想在这个网页上测试,就是一个例子。

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

if(myElement.tagName =="DIV"){

  alert("is a div");

}else{

  alert("is not a div");

}
/*What ever you may need to know the type write it in capitalised letters "OPTIO" ,"PARAGRAPH", "SPAN" AND whatever */

我在加强Andreq Frenkel的回答时,只是想增加一些答案,太长了。

我想到的是,如果一个要素是input,那么就能够检查这些要素,那么我认为,instance of是解决这一问题的最佳办法。

人们应当知道,instanceof 使用参考性平等,例如<代码>超文本部分,母窗将不等同于其<代码>iframe(或影子电解等)。

To handle that case, one should use checked element s own window s classes, something like:

<条码> 内容实例 页: 1





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签