是不是使用一个类有jquery和css的实践? [英] Is it poor practice to use a class with both jquery and css?

查看:117
本文介绍了是不是使用一个类有jquery和css的实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题有点像这个问题的反面:使用没有相应的CSS规则的CLASS属性是不好的吗?



我的代码中有几个地方我有一个列表,我想使用jQuery处理:

 < div class =myitemid =item1 > ...< / div> 
< div class =myitemid =item2> ...< / div>
< div class =myitemid =item3> ...< / div>
< div class =myitemid =item4> ...< / div>

我用一些javascript代码处理这些,例如:

  $(。myitem)。click(function(){...}); 

这些项目也有一个css样式,例如:

  .myitem {border:2px solid green; }这是一个大项目,有专门的人员使用ux的东西(包括黑客css和html) 



如果我改变这个方法来使用一个单独的类为jQuery的东西,所以我们不会不小心重命名或删除css类,认为我们只是使用图形外观?会为javascript引入一个新类引入更多的问题吗?有没有一些代码标准的诡计,我们可以用来帮助我们使用这个html?

解决方案

我个人喜欢分离风格从功能更好的可重用性。



您可以使用.myItem作为CSS,并添加类似.formProcess或任何它发生的用于在您的JS。



这样,如果UX人员删除或更改他们的类,你的函数仍然工作...并且如果存在类似的需求,他们可以在别处重用。


This question is a bit ilke the opposite of this one: Is it poor form to use CLASS attributes with no corresponding CSS rule?

There are several places in my code where I have a list of things that I want to process using jQuery:

<div class="myitem" id="item1">...</div>
<div class="myitem" id="item2">...</div>
<div class="myitem" id="item3">...</div>
<div class="myitem" id="item4">...</div>

I process these with some javascript code, e.g.:

$(".myitem").click(function() { ... });

These items also have a css style, e.g.:

.myitem { border: 2px solid green; }

It's a big project, with dedicated personell for working with ux stuff (including hacking css and html) and programmers that are more focussed on javascript and other code.

Should I change this to use a separate class for jquery stuff, so we don't accidentally rename or remove a css class, thinking we're just working with graphical appearance? Will introducing a new class for javascript introduce more problems? Is there some code standard trickery we could put in use to help us working with this html?

解决方案

I personally like to separate style from function for better reusability.

You might use .myItem for the CSS, and add a class like .formProcess or whatever it happens to be for use in your JS.

That way if the UX folks remove or change their classes, your functions still work... AND they can be reused elsewhere if a similar need exists.

这篇关于是不是使用一个类有jquery和css的实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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