使用 jQuery 生成/选择非标准 HTML 标签,是个好主意吗?

发布于 2024-10-26 10:22:04 字数 247 浏览 4 评论 0原文

我注意到 jQuery 可以创建和访问不存在/非标准的 HTML 标签。例如,

$('body').append('<fake></fake>').html('blah');
var foo = $('fake').html(); // foo === 'blah'

这会破坏某种验证吗?这是一个坏主意,还是有时这是有用的?主要问题是,虽然可以做,但应该做吗?

提前致谢!

I've noticed that jQuery can create, and access non-existent/non-standard HTML tags. For example,

$('body').append('<fake></fake>').html('blah');
var foo = $('fake').html(); // foo === 'blah'

Will this break in some kind of validation? Is it a bad idea, or are there times this is useful? The main question is, although it can be done, should it be done?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝海似她心 2024-11-02 10:22:04

您可以使用非标准 HTML 标签,并且大多数浏览器应该可以正常工作,这就是为什么您可以在无法识别它们的浏览器中使用 HTML5 标签,您所需要做的就是告诉它们如何设置它们的样式(特别是哪些标签是显示:块)。但我不建议这样做,原因有两个:首先它会破坏验证,其次您可能会使用一些稍后会添加到 HTML 中的标记,然后您的页面会突然停止在较新的浏览器中工作。

You can use non-standard HTML tags and most of the browsers should work fine, that's why you can use HTML5 tags in browsers that don't recognize them and all you need to do is tell them how to style them (particularly which tags are display: block). But I wouldn't recommend doing it for two reasons: first it breaks validation, and second you may use some tag that will later get added to HTML and suddenly your page stops working in newer browsers.

趴在窗边数星星i 2024-11-02 10:22:04

我看到的最大问题是,如果您创建了一个对您有用的标签,谁敢说它有一天不会成为标准?如果发生这种情况,它最终可能会发挥作用或获得您未预料到的样式,从而破坏您的代码。

The biggest issue I see with this is that if you create a tag that's useful to you, who's to say it won't someday become standard? If that happens it may end up playing a role or get styles that you don't anticipate, breaking your code.

清浅ˋ旧时光 2024-11-02 10:22:04

HTML 的规则确实规定,如果通过脚本进行操作,则结果在操作之前和之后都应该有效。

验证是达到目的的一种手段,所以如果它在某种程度上对你有用,那么我就不会太担心。也就是说,我不会在使用诸如 facebook 的 元素之类的东西时“偷偷”通过验证 - 我只是接受它并承认代码不是'有效。

The rules of HTML do say that if manipulated through script the result should be valid both before and after the manipulation.

Validation is a means to an end, so if it works for you in some way, then I wouldn't worry too much about it. That said, I wouldn't do it to "sneak" past validation while using something like facebook's <fb:fan /> element - I'd just suck it up and admit the code wasn't valid.

冷血 2024-11-02 10:22:04

HTML本身允许您使用任何您喜欢的标记。浏览器可能对未知标签有不同的反应(难道它们对已知标签不也是如此吗?),但一般的底线是它们忽略未知标签并尝试渲染其内容。

因此从技术上讲,没有什么可以阻止您使用 元素(比较 IE7 对 HTML5 页面和其中定义的新标签的处理方式)。 HTML 标准化一直是一个事后过程。浏览器供应商发明了标签,并在某个时候划定了界限,它被称为 HTMLx。

真正的问题是,您是否确实必须这样做。如果您关心 W3C 验证器是否喜欢您的文档。或者您是否关心其他程序员是否喜欢您的文档。

如果你能做同样的事情并保持在标准范围内,那就不值得这么麻烦了。

HTML as such allows you to use any markup you like. Browsers may react differently to unknown tags (and don't they to known ones, too?), but the general bottom line is that they ignore unknown tags and try to render their contents instead.

So technically, nothing is stopping you from using <fake> elements (compare what IE7 would do with an HTML5 page and the new tags defined there). HTML standardization has always been an after-the-fact process. Browser vendors invented tags and at some point the line was drawn and it was called HTMLx.

The real question is, if you positively must do it. And if you care whether the W3C validator likes your document or not. Or if you care whether your fellow programmers like your document or not.

If you can do the same and stay within the standard, it's not worth the hassle.

囚我心虐我身 2024-11-02 10:22:04

确实没有理由做这样的事情。更好的方法是使用类似的类

<p class = "my_class">

,然后执行类似

$('p.my_class').html('bah');

Edit:

操作。使用假标签不好的主要原因是它会使您的 HTML 无效,并且可能会在某些情况下破坏页面的呈现。浏览器,因为它们不知道如何处理您创建的标签(尽管大多数浏览器会将其视为某种 DIV)。

这就是这不好的主要原因,它只是违反了标准并导致难以维护的混乱代码,因为您必须解释自定义标签的用途。

如果您确实决定使用自定义标记,则可以将网页制作为有效的 XML 文件,然后使用 XSLT 将 XML 转换为有效的 HTML。但在这种情况下,我只会坚持上课。

There's really no reason to do something like this. The better way is to use classes like

<p class = "my_class">

And then do something like

$('p.my_class').html('bah');

Edit:

The main reason that it's bad to use fake tags is because it makes your HTML invalid and could screw up the rendering of your page on certain browsers since they don't know how to treat the tag you've created (though most would treat it as some kind of DIV).

That's the main reason this isn't good, it just breaks standards and leads to confusing code that is difficult to maintain because you have to explain what your custom tags are for.

If you were really determined to use custom tags, you could make your web page a valid XML file and then use XSLT to transform the XML into valid HTML. But in this case, I'd just stick with classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文