在 HTML 中适当分配 ID
我是 jQuery 新手(1.5 个月),我发现自己养成了一种我不太喜欢的 PHP + jQuery 习惯。我想知道是否有人有建议让事情变得更优雅。考虑一下我通过执行简单的 SQL 查询生成并使用 PHP 进行回显的以下 HTML:
<div id='boat_1' class='boat speedboat'></div>
<div id='boat_2' class='boat dory'></div>
<div id='car_1' class='car racecar'></div>
<div id='car_2' class='car racecar'></div>
如您所见,我选择了遵循 {type}_ 的
。现在,在 jQuery 中,假设我想将点击处理程序绑定到这些 car div。记住它们是动态的并且可以有任意数量,我会执行:id
命名约定{id}
$(".car").bind('click', function(){
//do something
});
但通常, //do some
想要知道哪辆车触发了点击事件。此外,//do some
可能需要将类型(船或汽车)和 id
分开才能执行 POST 操作并写回数据库...... (例如)
我现在正在做的查找 id(以及我需要的其他唯一信息)的操作很简单,但对我来说,它有点味道:
$(".car").bind(function(){
var id = $(this).attr("id").replace("car_", "");
});
有人可能会建议使用 class 属性,因为您不必担心独特性。但由于类名不是单一的(如我所示,每个元素可以有多个),因此我不认为这是一种候选解决方案。
因为 id 在整个文档中必须是唯一的,所以这是我已经解决的解决方案。我知道如果可以使用多个同名 id, getElementById() 会中断,但有时我想知道如果 ids 不必是唯一的(只要它们有不同的父项),这是否会有好处。
你怎么做?
我应该使用 HTML5 的 data-*
吗?
谢谢!
I'm new to jQuery (1.5 months) and I've found myself developing a PHP + jQuery habit that I don't particularly like. I'm wondering if anyone has suggestions to make things more elegant. Consider the following HTML that I've generated by performing a simple SQL query and echoed with PHP:
<div id='boat_1' class='boat speedboat'></div>
<div id='boat_2' class='boat dory'></div>
<div id='car_1' class='car racecar'></div>
<div id='car_2' class='car racecar'></div>
As you can see, I've opted for an id
naming convention that follows {type}_{id}
. Now, in jQuery, Assume I want to bind a click handler to these car divs. Remembering that they are dynamic and there could be any number of them, I'd perform:
$(".car").bind('click', function(){
//do something
});
But typically, that //do something
would like to know which car fired the click event. Furthermore, that //do something
will probably need to seperate the type (boat or car) and the id
to perform a POST operation and write back to the database...(for example)
What I'm doing now to find the id (and other unique information as I need it) is simple, but to me, it smells:
$(".car").bind(function(){
var id = $(this).attr("id").replace("car_", "");
});
One might suggest to use the class attribute instead because you don't have to worry about uniqueness. But because class names are not singular (there can be more than one per element as I've shown), I don't see this as a candidate solution.
Because id's must be unique in the entire document, this is the solution I've settled for. I'm aware getElementById() would break if multiple ids of the same name were made possible but sometimes I wonder if it would be beneficial if ids didn't have to be unique provided they have different parents.
How do you do it?
Should I use HTML5's data-*
?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不喜欢您的方法的哪些方面?我不希望使用
bind()
而是使用click()
。另外,如果您愿意,您可以链接这些类并更改//do some
以改为用 _ 分割 id 并返回数组中的最终[1]
项? :)你的方法似乎可以很好地获取数字 ID! :)
What don't you like about your method? I'd look not to use
bind()
but to useclick()
. Also if you want, you can chain the classes and alter your//do something
to instead split the id by _ and return the final[1]
item in the array? :)Your method seems fine to get the numeric id! :)
说实话,这就是我会做的。
然而,我会以相反的方式过滤它,并使它成为一个像 这个人正确建议的函数< /a> 并从字符串中取出数字:
To be honest, that's what I'd do.
I would however filter it the other way around and make it a function like this guy correctly suggested and pull the number out of the string:
是的。
像这样渲染 HTML:
Jquery handler
Jquery Data function
Yes.
Render HTML like this:
Jquery handler
Jquery Data function
我认为你的方法看起来很适合你正在做的事情,但我会指出,在 HTML 4 中,你可以使用
rel
属性,该属性实际上未使用,但是是有效的 HTML(因此它赢得了不会像 HTML4 解析器中的data-*
那样触发警告)。我使用它,就像您建议使用 class 属性一样,但没有修改我的类的副作用。I think your method looks good for what you're doing, but I'll point out that in HTML 4 you can use the
rel
attribute, which is practically unused, but is valid HTML (so it won't trigger warnings likedata-*
will in HTML4 parsers). I use it, just like you proposed using the class attribute, but without the side-effect of munging my classes.