jQuery检测标签是否自动关闭 [英] JQuery detect if a tag is self closing

查看:66
本文介绍了jQuery检测标签是否自动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以检测标签是否像图像标签一样通过JQuery自关闭?动态的东西不仅是图像标签.

Is there a way to detect if a tag is self closing with JQuery like an image tag? Something dynamic not just an image tag.

if ($('.selector')[0].tagName.toLowerCase()=="img") {
    // do something
}

推荐答案

您是否需要区分空元素和自闭合元素?如果没有,您可以使用:

Do you need to distinguish between an element that's empty vs self-closing? If not, you can use:

if ($(".selector").is(":empty")) {
  // do something
}

但是,如果您要谈论检测给定HTML标记是否为空,则不能以编程方式进行操作,因为您不会停止创建无效的HTML,因此需要针对DTD对其进行验证以找出答案.

But if you're talking about detecting whether a given HTML tag should be empty or not, no you can't do that programmatically because you're not stopped from creating invalid HTML and you need to validate it against a DTD to find out.

这篇关于jQuery检测标签是否自动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆