Is it ok to put HTML into HTML attributes?
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 & 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 >
is recommended to prevent problems with broken HTML parsers. Source
Escaping &
to &
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 "
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.
精彩评论