When jquery selects and deletes table rows with check boxes, it is highlighted. _ jquery-js tutorial

Source: Internet
Author: User
In actual applications, check boxes may appear in the table. When deleting a table, delete the records of the rows where the check boxes are located. You can add a special effect here. When you click a row and select the check box of the row, the background color of the row is highlighted. Using jquery technology to operate the table is simple, with the jquery syntax, you can easily change the color of a table by row, and hover and highlight it. In actual applications, check boxes may appear in the table. When you delete a table, delete the record of the row where the check box is located. You can add a special effect here. Click a row and select the check box of the row. the background color of the row is also highlighted. This makes people feel very good.

The effect is as follows:

Here I have two functions:
Function 1. Click a row. The check box of the row is selected and the background color is changed.
Function 2. Click "select all" or "do not select all" to change the line color.
I encapsulated the two functions into the js file and introduced them when using them.
Let's take a look at the CSS code. I encapsulated it into a css file.

The Code is as follows:


. Selected {
Background: # FF6500;
Color: # fff;
}


Check the js file code:
Function 1 code:

The Code is as follows:


/**
* Set the background color in the table containing the check box.
*/
$ (Document). ready (function ()
{
/**
* The background color is changed when the table row is clicked.
*/
$ ("# Tablight tr: gt (0)"). click (function () // after obtaining the 2nd rows
{
If ($ (this). hasClass ("selected") // you can check whether the image is selected.
{
$ (This). removeClass ("selected"). find (": checkbox"). attr ("checked", false); // select the remove Style
}
Else // set the selected
{
$ (This). addClass ("selected"). find (": checkbox"). attr ("checked", true); // Add a style if it is not selected
}
});
});


Function 2 code:

The Code is as follows:


/**
* Change the background color after you click "select all" or "reselect ".
*/
Function setColor () // sets the background color of tr.
{
Var checkboxs = $ ("# tablight tr: gt (0) input [type = checkbox]"); // get all check boxes
Var boxeds = $ ("# tablight tr: gt (0) input [type = checkbox]: checked"); // obtain the selected check box
If (boxeds. length> 0)
{
Checkboxs. parent (). parent (). addClass ("selected"); // check box in td
}
Else
{
Checkboxs. parent (). parent (). removeClass ("selected ");
}
}


If you want the code to take effect, you need to add the id attribute to the table. The attribute value is "tablight". If you select all or do not select all, call the setColor method.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.