Use jQuery to operate on an HTML table.

Source: Internet
Author: User
This article describes how to use jQuery to parse table tables in HTML, including Tips for getting the total number of rows in a table. For more information, see:

Let's take a look at the main parts:

HTML:

.td_back_color {   background-color: red; } .td_center {   text-align: center;   font-weight:bold; } 


CSS:

 
 ...... 
  
2013 Spring Festival Gala Income and Expenditure Statistics
Name Dinner KTV Barbecue Expenditure Payable
Weekly Competition

JavaScript:

Var data = [{picture :" http://ww1.sinaimg.cn/mw690/51baa38egw1dzl6l2hjchj.jpg ", Name:" ", leftTime:" February 5 ", phone:" 186 *** 2296 ", qq:" ", workedIn:" Zhejiang Hangzhou ", cost: 200, project: "dinner, KTV, barbecue "},........ // initialize shuju var init = function () {var tbl_count = $ ("# tbl_count tr: gt (0 )"); // eat var meal_text =$ ("# tbl_count tr: first th: eq (1 )"). text (); // KTV var ktv_text = $ ("# tbl_count tr: first th: eq (2 )"). text (); // barbecue var bbq_text = $ ("# tbl_count tr: first th: eq (3 )"). text (); $. Each (tbl_count, function (I, v) {var tr_info = tbl_count.eq (I); for (var I = 0; I <data. length; I ++) {var data_info = data [I]; // determine if (data_info.name = tr_info.find ("th: first") with the same name "). text () {if (data_info.project.indexOf (meal_text )! =-1) {tr_info.find ("td: eq (0)"). addClass ("td_back_color") ;}if (data_info.project.indexOf (ktv_text )! =-1) {tr_info.find ("td: eq (1)"). addClass ("td_back_color") ;}if (data_info.project.indexOf (bbq_text )! =-1) {tr_info.find ("td: eq (2 )"). addClass ("td_back_color");} tr_info.find ("td: eq (3 )"). addClass ("td_center "). text ("¥" + data_info.cost );}}});};

PS: jQuery obtains the total number of rows in a table.

var rowCount = $('#myTable tr').length; var rowCount = $('#myTable >tbody >tr').length; $("#myTable").attr('rows').length; var rowCount = $('table#myTable:last').index() + 1; //Helper function that gets a count of all the rows  in a table body  $.fn.rowCount = function() {   return $('tr', $(this).find('tbody')).length; };  // USAGE:  var rowCount = $('#productTypesTable').rowCount(); alert(jQuery("#jtkList").find("table").eq(0).find("tr").length); 
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.