简体   繁体   English

跟踪Jquery和CSS标识符的最佳方法是什么?

[英]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. 如果不清楚,请告诉我,谢谢您的帮助。

  • Have a consistent naming scheme for your "sections" in terms of case and meaning 在大小写和含义方面为您的“部分”制定一致的命名方案
  • Keep id and class names the same both for CSS and JS to avoid the confusion. CSS和JS的id和class名称应保持相同,以避免混淆。
  • Relating to semantic code, you should give meaning to the class/id names as well. 关于语义代码,您也应该给类/ id名称赋予含义。 This helps you remember what you are adding code for. 这可以帮助您记住要为其添加代码的内容。 Don't use something like " 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; 这是我的建议;

  • Always use unique IDs. 始终使用唯一的ID。 IDs should be unique, given to a specific element or selector only while classes can be given to multiple selectors. ID应该是唯一的,仅当可以将类分配给多个选择器时才分配给特定的元素或选择器。
  • No need to use same class for JavaScript and CSS both since multiple classes can be assigned to a selector like <div class="class1 class2"> 无需为JavaScript和CSS使用相同的类,因为可以将多个类分配给选择器,例如<div class="class1 class2">
  • Use a style-sheet attachment for common items appear in multiple pages so as to limit Embedded and Inline styles. 对于在多页中显示的常见项目,请使用样式表附件,以限制嵌入式和嵌入式样式。

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.

相关问题 在 Redux 中管理您的操作的最佳方法是什么? - What is the best way to manage your actions in Redux? 如何实现复选框的属性以在不使用Grid对象或jQuery.Data(Input)的情况下跟踪表中每一行的更改? - How to implement properties of checkbox to keep track of your changes on each line in your table without employing Grid object or jQuery.Data(Input)? 将自定义JS文件和字体添加到自己的UIkit发行版中的最佳方法是什么 - What is the best way to add your custom JS files and fonts to your own distribution of UIkit 跟踪实时通知发送给用户的最佳方法是什么? - What's the best way to keep track of live notifications to send to a user? 进行原型制作时跟踪javascript对象引用的最佳方法是什么 - What the best way to keep track of a javascript object reference when prototyping 在MongoDB中跟踪文档属性更改的最佳方法是什么? - What is the best way to keep track of changes of a document's property in MongoDB? 在HTML页面中订购js文件的最佳方法? - Best way to order your js files in your HTML page? 如何在使用jquery / css动态显示周围元素时保持当前屏幕位置 - How to keep your current screen position while dynamically showing surrounding elements with jquery / css 在 React 中保护路由的最佳选择是什么? - What is best option to guard your routes in React? 你最喜欢在djangoapp中测试javascript的方法是什么? - What's your favorite way to test the javascript in your djangoapp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM