什么是最好的HTML属性用于存储信息以供jQuery解析? [英] What's the best HTML attribute to use to store information for jQuery to parse?

查看:98
本文介绍了什么是最好的HTML属性用于存储信息以供jQuery解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须支持IE6,且必须验证与XHTML Strict 1.0!

这很难解释...

我使用通用类名来在相关联的元素上启动插件功能。
我也想要有一个与存储在属性中的元素相关的选项。

I'm using a generic class name to initiate a plugin feature on an associated element. I also want to have options associated with the element stored in an attribute as well.

<a href="url.com" class="popup" rel="900x900" >My Link</a>

有了这个,jQuery会寻找所有有'popup'的元素,并解析rel值尺寸的弹出窗口,并启动popup()函数每当这个链接点击一个窗口大小w = 900 h = 900

With this, jQuery will look for all elements that have 'popup' and parse the rel value for the dimensions of the popup and initiate the popup() function whenever this link is clicked with a window the size w=900 h=900

但我需要采取这一步进一步因为我想要更多选项...

but I need to take this a step further because I want to have more options...

<a href="url.com" class="popup" rel="900x900_scroll_tool_menu" >My Link</a>

我不知道如果使用rel属性是因为我也想使用对于没有rel =属性的其他元素。

I'm not sure if using the rel attribute is the place for this because I also want to use this on other elements that dont have a rel= attribute.

所以我正在考虑为此使用类...我想出了这个:

So I was thinking using classes for this too... I came up with this:

 <a href="url.com" class="popup opt_dim-900x900_scroll_tool_menu" >My Link</a>
 <img src="pic.gif" class="popup opt_dim-150x200_location" >My Link</a>

从这个看起来,选项可以得到非常长似乎确定,但也许有更好的东西。

From the looks of this the options can get VERY long, using class seems ok, but maybe there's something better..

你认为哪种更好?你有另一个想法吗?我想将这些选项存储在某些html属性中。

Which way do you think is better? Do you have another idea for this? I want to store the options in some html attribute.

谢谢!

UPDATE

我不断提醒,有十几种方法可以在Javascript中做任何事情,就这里的解决方案而言,我后来改变了对html5数据属性的正确答案,现在ie6不是一个问题,似乎是最好的方法。

I am continually reminded that there are a dozen ways to do anything in Javascript, in terms of the solutions here I later changed the correct answer to the html5 data attribute, which now that ie6 isnt an issue, seems like the best method.

最好,因为它使用标准功能,并避免任何我试图做的类名称的黑客。当然类名仍然非常灵活,但是该解决方案不是语义的,也不遵循将视图与行为分离的最佳实践。

Best, because it uses standard features and avoids any of the hackery I was trying to do with class names. Sure classnames are extremely flexible still, but that solution isn't semantic, nor does it follow best practice of separating views from behavior.

推荐答案

p>如果你可以使用HTML5,那么使用data- *属性 - 它们是为这个问题设计的。

If you can use HTML5, then use data-* attributes -- they were designed for exactly this problem.

<div data-tool-menu="900x900">

http://ejohn.org/blog/html-5-data-attributes/

这篇关于什么是最好的HTML属性用于存储信息以供jQuery解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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