The realization method of table interlacing js and jquery

Source: Internet
Author: User

Here is a description of the form of alternate colors of the method, we first to introduce the method of jquery, and then a simple introduction to the implementation of JS method.

The code is as follows Copy Code

<script type= "Text/javascript" >
$ (document). Ready (function () {
$ (". Table tr"). MouseOver (function () {//If the mouse moves over the TR of a table with class table, execute functions
$ (this). addclass ("hover"); Mouseout (function () {//) adds a class value to this row hover and executes functions when the mouse is out
$ (this). Removeclass ("hover");}) Remove class for this line
$ (". Table Tr:even"). AddClass ("even"); Add the class value to Alt for the even row of a table with class
});
</script>
HTML code

HTML code
<table class= "table" border= "0" cellpadding= "0" cellspacing= "0" width= "50%" >
<!--use class= "table" to identify the table you want to use this effect-->
<thead>
<tr>
<th> name </th>
<th> Age </th>
<th>MSN</th>
<th>Email</th>
</tr>
</thead>
<tr>
<td>Owen</td>
<td>30</td>
<td>owen.net@hotmail.com</td>
<td><a href= "/css/" >css</a></td>
</tr>
<tr>
<td>Owen</td>
<td>30</td>
<td>owen.net@hotmail.com</td>
<td><a href= "/css/" >css</a></td>
</tr>
<tr>
<td>Owen</td>
<td>30</td>
<td>owen.net@hotmail.com</td>
<td><a href= "/css/" >css</a></td>
</tr>
<tr>
<td>Owen</td>
<td>30</td>
<td>owen.net@hotmail.com</td>
<td><a href= "/css/" >css</a></td>
</tr>
<tr>
<td>Owen</td>
<td>30</td>
<td>owen.net@hotmail.com</td>
<td><a href= "/css/" >css</a></td>
</tr>
<tr>
<td>Owen</td>
<td>30</td>
<td>owen.net@hotmail.com</td>
<td><a href= "/css/" >css</a></td>
</tr>
</table>


JS method, using class to find elements

The code is as follows Copy Code

/**
* Get elements based on sign signature and class name
* @param tagName Signature
* @param elementclassname Element class name
* @return Sign signature and an array of elements with the given parameter as the class name
*/
function Getelementsbytagnameandclassname (tagname,elementclassname) {

var array=new array ();

var elements=document.getelementsbytagname (tagName);

for (Var i=0;i<elements.length;i++) {

if (elements[i].classname==elementclassname) {

Array.push (Elements[i]);
}
}

return array;
}

/**
* Table alternate color, specify the mouse to move to the background color of the current row, remove the background color of the current row
* @param the class attribute name of the ClassName table
* @param oddbackgroundcolor table odd Row background color
* @param evenbackgroundcolor table even row background color
* @param Mouseoverbackgroundcolor mouse to the background color of the current line
*/
function Tablerowbackgroundcolor (classname,oddbackgroundcolor,evenbackgroundcolor,mouseoverbackgroundcolor) {

var tables=getelementsbytagnameandclassname ("table", className);

for (Var i=0;i<tables.length;i++) {

var rows=tables[i].rows;

var roworiginalcolor= "";

for (Var j=1;j<rows.length;j++) {

Rows[j].style.backgroundcolor = j%2==0? Oddbackgroundcolor:evenbackgroundcolor;

The mouse moves to the current line, specifying the background color of the current row.
Rows[j].onmouseover=function () {

Roworiginalcolor=this.style.backgroundcolor;

This.style.backgroundcolor=mouseoverbackgroundcolor
}

The mouse moves out of the current line, specifying that the background color of the current row is the original color of the row.
Rows[j].onmouseout=function () {

This.style.backgroundcolor=roworiginalcolor;
}

}
}
}

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.