Code for merging cells with the same content based on JQuery

Source: Internet
Author: User

During front-end web development, you will often encounter pages to form or make some tables. For example, if the same content is to be displayed in the same cell, the general method is to set the table in the table, but this method will increase the page load speed and affect the page loading speed. But if there is a lot of css styles that cannot be controlled by DIV, what should we do? Let's just use JQuery to share the cells with the same content in a table. The code here will be useful to you as follows:

Header JQuery code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
JQuery. fn. rowspan = function (codecomx) {// a small JQuery plug-in encapsulated
Return this. each (function (){
Var that;
$ ('Tr', this). each (function (row ){
$ ('Td: eq ('+ codecomx +') ', this). filter (': visible '). each (function (col ){
If (that! = Null & lt; this).html () = Hangzhou (that).html ()){
Rowspan = $ (that). attr ("rowSpan ");
If (rowspan = undefined ){
$ (That). attr ("rowSpan", 1 );
Rowspan = $ (that). attr ("rowSpan ");}
Rowspan = Number (rowspan) + 1;
$ (That). attr ("rowSpan", rowspan );
$ (This). hide ();
} Else {
That = this;
}
});
});
});
}
$ (Function (){
$ ("# Table1"). rowspan (0); // The input parameter indicates that the number of columns starts from 0. If any column has the same content, enter the corresponding column value.
$ ("# Table1"). rowspan (2 );
});
</Script>

Add a table to the body.
Copy codeThe Code is as follows:
<Body>
<Table id = "table1" border = "1" cellpadding = "5" cellspacing = "0" width = "300px">
<Tr>
<Td> 1 </td>
<Td> 2 </td>
<Td> 3 </td>
<Td> 4 </td>
</Tr>

<Tr>
<Td> 1 </td>
<Td> f </td>
<Td> 3 </td>
<Td> s </td>
</Tr>
</Table>
</Body>

IE Effect

Firefox Effect

More front-end development can focus on the UI front-end

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.