data- *属性是否可以包含HTML标签? [英] Can data-* attribute contain HTML tags?

查看:117
本文介绍了data- *属性是否可以包含HTML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即。 < img src =world.jpgdata-title =Hello World!< br />什么给予?/>

解决方案

据我了解指导原则,它基本上是有效的,但最好使用HTML实体。

HTML 4引用


您也应该转义&因为在cdata属性值内允许实体引用,所以属性值内。另外,你应该逃避>作为>以避免旧版用户代理出现问题,这些用户代理错误地将此视为引用属性值中的此字符时标记的结尾。


HTML 5参考:


除非另有指定,否则HTML元素上的属性可能包含任何字符串值,包括空字符串。除了明确说明的情况外,对于可以在这些属性中指定哪些文本没有限制。


所以最好的做法是@tdammers已经说过,是为了逃避这些角色(引用W3C的引用)。
$ b $ ul

  • & amp; 来表示& 标志。
  • & lt; 来表示< 标志。

  • & gt; 表示> 标志。

  • & quot; to代表标记。



  • 将被用作HTML。


    I.E. <img src="world.jpg" data-title="Hello World!<br/>What gives?"/>

    解决方案

    As far as I understand the guidelines, it is basically valid, but it's better to use HTML entities.

    From the HTML 4 reference:

    You should also escape & within attribute values since entity references are allowed within cdata attribute values. In addition, you should escape > as > to avoid problems with older user agents that incorrectly perceive this as the end of a tag when coming across this character in quoted attribute values.

    From the HTML 5 reference:

    Except where otherwise specified, attributes on HTML elements may have any string value, including the empty string. Except where explicitly stated, there is no restriction on what text can be specified in such attributes.

    So the best thing to do, as @tdammers already says, is to escape these characters (quoting the W3C reference)

    • &amp; to represent the & sign.
    • &lt; to represent the < sign.
    • &gt; to represent the > sign.
    • &quot; to represent the " mark.

    and decoding them from their entity values if they are to be used as HTML.

    这篇关于data- *属性是否可以包含HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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