[英]What is the best way keep track of your identifiers for your Jquery and CSS?
This is more of an organisation question concerning Jquery. 这更多是关于Jquery的组织问题。
Using the following simple example: 使用以下简单示例:
<a href="#" class="click">click me</a>
<script>
$(document).ready(function(){
$('.click').on('click',function(){
alert('Clicked!');
});
});
</script>
Now multiply this by a 1000 on your website, it can quickly get messy. 现在,在您的网站上乘以1000,它很快就会变得混乱。 You also can forget which classes you attributed for Jquery or for your CSS. 您还可以忘记为Jquery或CSS归属的类。 I usually try to avoid mixing both, so I add my classes for CSS and others for the Jquery, in case I change I the layout or use that section somewhere else, ... I can change the CSS. 我通常会尽量避免两者混用,所以我为CSS添加了我的类,为Jquery添加了其他类,以防万一我更改布局或在其他地方使用该部分,...我可以更改CSS。 But how to do you remember which one is for CSS or Jquery? 但是,如何记住CSS或Jquery中的哪一个呢? Do you name your classes "JSclassName" or something like that? 您将类命名为“ JSclassName”还是类似的名称?
Let me know if I'm not clear and thanks for your help. 如果不清楚,请告诉我,谢谢您的帮助。
centeredContainer
" or " thisIsClickable
". 不要使用“ centeredContainer
”或“ thisIsClickable
”之类的东西。 Example: 例:
<section class="weatherWidget">
<ul class="forecast">
<li class="vicinity">
...
<ul class="news">
<li class="region">
...
I also namespace my CSS and JS to avoid conflicts. 我还为CSS和JS命名空间以避免冲突。 The following code ensures that only weather widget contents are referenced by the code: 以下代码确保该代码仅引用天气窗口小部件内容:
//this click event is only for weatherWidget vicinity
$('.weatherWidget .vicinity').on('click',function{...});
//this CSS is only for weatherWidget regions
.weatherWidget .region{...}
Also, although I don't really use them nor endorse using them, LESS and SASS frameworks can also help in terms of avoiding redundant CSS code by using variables. 另外,尽管我并没有真正使用它们,也不赞成使用它们,但是LESS和SASS框架在避免使用变量的冗余CSS代码方面也可以提供帮助。
First, give your identifiers more meaningful names. 首先,为您的标识符赋予更有意义的名称。 In my opinion, click
is not a ideal one. 我认为click
不是理想的选择。 A button is clickable, so is a anchor, even a div
element. 按钮是可单击的,锚点甚至是div
元素都是可单击的。
Second, if you do have hundreds of identifiers, try to use namespace to distinguish CSS/JavaScript identifiers. 其次,如果您确实有数百个标识符,请尝试使用名称空间来区分CSS / JavaScript标识符。 But I think this is not necessary, using the CSS identifier in your JavaScript is natural, a meaningful name is in the first position. 但是我认为这不是必须的,因为在JavaScript中自然使用CSS标识符,一个有意义的名称就放在第一位。
<style>
.ui-cart-button{ };
</style>
<a href="#" class="js-add-to-cart ui-cart-button">Buy</a>
Namespace is a good way to make your code cleaner and more readable, I recommend you to have a look at the source code of jQueryUI. 命名空间是使代码更整洁和更具可读性的好方法,我建议您看一下jQueryUI的源代码。
Here are my suggestions; 这是我的建议;
<div class="class1 class2">
无需为JavaScript和CSS使用相同的类,因为可以将多个类分配给选择器,例如<div class="class1 class2">
Links below will suggest some guidelines for you. 以下链接将为您建议一些准则。
30 CSS Best Practices for Beginners 30个初学者CSS最佳实践
15 Best CSS Practices to Make Your Life Easier 15种最佳CSS做法,让您的生活更轻松
10 Best CSS Practices to Improve Your Code 改进代码的10种最佳CSS做法
All the above given information is to let you know about using CSS. 上面提供的所有信息都是为了让您了解CSS的使用。 From all these info, you may formulate the best option ideal for your needs. 从所有这些信息中,您可以制定出满足您需求的最佳选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.