Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

javascript - When do you use "class" versus "id"?

I'm using Bootstrap to style my table, which is a basic to do list. I wanted to add a form input in one cell and check button in the cell directly next to it on the right. I wanted to use id="check-button". I think when I run my JavaScript and jQuery, I would need to use class if I want to add more buttons. I am now setting my HTML and JS.

Here is what I have for the HTML:

<div class = "container">
  <div class="col-md-8">
    <table class="table table-hover">
      <thead>
        <tr>
          <td>To Do</td>
          <td> Status</td>
       </tr>
    </thead>
    <tbody>
      <tr>
        <td>  
          <div class="input-group">
             <span class="input-group-addon">
             <input type="text" class="form-control" aria-label="..." placeholder="Text input" id="disabledInput">
             </span> 
           </div><!-- /input-group -->
        </td>
        <td id="check-button">
          <input type="checkbox" aria-label="...">
        </td>
      </tr>
    </tbody>
  </table>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mentioned in comments id should be used as a unique identifier. To help enforce this your HTML Mark-up will not be valid if there are repeating id= attributes with the same value on the same page.

When it comes to deciding what to use, consider how the element will be selected within the JS/jQuery. Is the jQuery going to use that element and ONLY that element when processing? If so, then id is fine, if the element is part of a group and you would like to re-use the jQuery when something happens to any item in that group then using a class= is more appropriate.

id and class can also be used together. Say you want something to happen whenever a group is clicked, but also want to know exactly which item in the group was clicked, you could do something like:

HTML:

<div id="1" class="clickable"> Click Here!! </div>
<div id="2" class="clickable"> Click Here!! </div>
<div id="3" class="clickable"> Click Here!! </div>
<div id="Clicked"></div>

jQuery:

$('.clickable').click(function(){
   // Reset the color of all clickable elements
   $('.clickable').css("background-color","white");
   // Display which element we clicked
   $('#Clicked').html('You Clicked Div: ' +this.id);
   // Set the background color of the clicked element
   $(this).css("background-color","lightgreen");
});

JSFiddle

In this example the first three divs are part of a 'group' by using a common class="clickable" in the jQuery I can then trigger an event for any of these elements without having to duplicate code.

The .clickable selector is a Class Selector which means that it will be triggered for any element with class="clickable", this does not have to be a div.

The #Clicked selector is a Id Selector which will select the first element matching id="Clicked"

Once the .click event has been triggered the this.id can be used to get the exact element we clicked on out of the three, $(this) can be used to manipulate the clicked element and $('.clickable') can be used to manipulate all elements grouped by the class attribute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...