Is it poor practice to use a class with both jquer

2019-05-24 04:26发布

问题:

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?

回答1:

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.



回答2:

I like to use the SMACCS method where the class is used to denote a module which should be a self contained group of html elements. The class is then used to identify the module to css and javascript.



标签: jquery css class