开发者

Is it ok to put HTML into HTML attributes?

开发者 https://www.devze.com 2023-02-01 05:08 出处:网络
I want a place to store markup for my tooltips. I thought of data-* attributes, it works, but I wonder if its wrong or may cause problems ...

I want a place to store markup for my tooltips. I thought of data-* attributes, it works, but I wonder if its wrong or may cause problems ...

<a href="#" 
   title="You can 开发者_运维知识库edit project details like name & description" 
   data-tip="<h6>Edit Project</h6><p>You can edit project details like name &amp; description here</p>">
   Edit
</a>


This is fine; a minor side-effect is that data-* properties are invalid in HTML 4. There's nothing you can do about this except (mis)using some other existing property, which is usually worse than not passing validation.

Escaping > to &gt; is recommended to prevent problems with broken HTML parsers. Source

Escaping & to &amp; as you already do is necessary for it to be valid HTML (if the & is not followed by a space and thus forms an entity, cheers @bobince)

Obviously, you must escape " to &quot; to prevent the HTML from breaking.


Attributes can contain parsed data (including character references), so holding HTML in a custom attribute is fine.

The problems I can see is with terminating the string early. So long as you encode quotes, you will be fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消