[英]The Best Way To Represent key/value Pairs In HTML Class Names
I am binding things to HTML objects using javascript and need a slightly more sophisticated mechanism than just class names. 我使用javascript将事物绑定到HTML对象,并且需要比类名更复杂的机制。
My first approach was to use class names like structure declarations where the binding is on name position as so: 我的第一种方法是使用类名称,如结构声明,其中绑定位于名称位置,如下所示:
Definition = identifier: foo bar baz
Encoding some class name with this definition gives 用这个定义编码一些类名给出
<p id="someid" class="identifier bish bash bosh">
<div id="anotherid" class="identifier heebie jeebie">
This can then be read as: 这可以理解为:
{foo: bish, bar: bash, baz: bosh};
{foo: heebie, bar: jeebie}
Note that the bindings have different numbers of parameters. 请注意,绑定具有不同数量的参数。
This is very fragile. 这非常脆弱。 I can't decorate any html elements with additional names (for instance to add jquery behaviours like drag and drop, etc, etc) and is generally a bit shoddy...
我无法使用其他名称来装饰任何html元素(例如添加拖放等jquery行为等)并且通常有点粗制滥造......
What I would like to do is represent them as key value pairs like so: 我想做的是将它们表示为键值对,如下所示:
<p id="someid" class="id{foo:bish} id{bar:bash} id{baz:bosh} random class">
<div id="anotherid" class="ui-draggable id{bar:jeebie} id{foo:heebie}">
Which allows me to have random positioning and random length of class names on elements provided my id is a sorta GUID (but I can 'make that so' manually easily enough...) 这允许我在元素上有随机定位和随机长度的类名,前提是我的id是一个sorta GUID(但我可以'手动轻松地做到这一点'......)
Now, in theory , classnames are CDATA, so everything should be tickety-boo, but I hae ma doots! 现在, 从理论上讲 ,类名是CDATA,所以一切都应该是嘀嗒声,但我喜欢麻烦! particularly on the old cross-browser side...
特别是在旧的跨浏览器方面......
Advice, suggestions, prior art, best practice on the best syntax for KV pairs in class names all gratefully received. 所有关于KV对的最佳语法的建议,建议,现有技术,最佳实践都感激不尽。
HTML5 is going to add data-* attributes for just this purpose, and these already work in every browser today (but might send some validators for a loop). HTML5将为此目的添加data- *属性,这些已经在今天的每个浏览器中都有效(但可能会为循环发送一些验证器)。
http://ejohn.org/blog/html-5-data-attributes/ http://ejohn.org/blog/html-5-data-attributes/
<li class="user" data-name="John Resig" data-city="Boston"
data-lang="js" data-food="Bacon">
<b>John says:</b> <span>Hello, how are you?</span>
</li>
The allowed className characters will help you determine what to use. 允许的className字符将帮助您确定要使用的内容。
For me, I would lean towards something like: 对我来说,我会倾向于:
keys: soft, hard, wet, hot
values: foo, bar, baz, fuzz
class="soft_bar wet_foo hot_fuzz"
In particular, the braces {,} won't work as they are used in the CSS stylesheets to mark blocks of style properties. 特别是,大括号{,}将不起作用,因为它们在CSS样式表中用于标记样式属性的块。
有必要在这个类?中,你可以在new attr中创建key / value的数据
$("#someid").attr("key_val", "id{foo:bish} id{bar:bash} id{baz:bosh}");
If you're not concerned about valid HTML, you could just use arbitrary attribute names instead of trying to hijack the "class" attribute. 如果您不关心有效的HTML,您可以使用任意属性名称而不是试图劫持“class”属性。
<p id="someid" foo="bish" bar="bash" baz="bosh">
And access their values from jQuery via: 并通过以下方式从jQuery访问它们的值:
$("#someid").attr("foo"); // "bish"
Could you surround the object in question with a div or span? 你能用div或span包围有问题的物体吗? Then use the id and/or class attributes to store the name-value pairs and access them with parent property.
然后使用id和/或class属性存储名称 - 值对并使用parent属性访问它们。
<span id="foo_bar_baz" class="bish_bash_bosh"><p id="someid"></span>
This allows you to do whatever you want to the original object. 这允许您对原始对象执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.