最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery. Select all elements with a class but without style="display:none;" attribute - Stack Over

programmeradmin0浏览0评论

I have the following class, which selects what's inside of the td.lineitemtotal cells and uses it in the getTotal function to get a total of the cells. But, I don't want the function to use the lines that are in a tr.line_item_row with style="display:none;" attribute.

$(document).ready(function() {
  var line = $('.item');
  // so the line totals are calculated on page load
  getLineItemTotals(line);

  var line_totals = $('.lineitemtotal');
  getTotal(line_totals); // So the total is calculated on page load.
});

// get invoce grand total
function getTotal(lines) {
  var total = 0;
  $.each(lines, function(){
    total += parseFloat($(this).html());
  });
  $('#total-price').html('$' + total.toFixed(2));
}

I have the following class, which selects what's inside of the td.lineitemtotal cells and uses it in the getTotal function to get a total of the cells. But, I don't want the function to use the lines that are in a tr.line_item_row with style="display:none;" attribute.

$(document).ready(function() {
  var line = $('.item');
  // so the line totals are calculated on page load
  getLineItemTotals(line);

  var line_totals = $('.lineitemtotal');
  getTotal(line_totals); // So the total is calculated on page load.
});

// get invoce grand total
function getTotal(lines) {
  var total = 0;
  $.each(lines, function(){
    total += parseFloat($(this).html());
  });
  $('#total-price').html('$' + total.toFixed(2));
}
Share Improve this question asked Sep 20, 2012 at 17:08 leonelleonel 10.2k21 gold badges87 silver badges129 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 11

Do you want this ?

$('.lineitemtotal:visible');

This set contains the not hidden elements having class lineitemtotal.

var line_totals = $('.lineitemtotal:not([style*="display:none"])');

On your selector include the :visible selector:

$('.lineitemtotal:visible');

If you know for certain that the attribute will always be style="display:none;" you can use an attribute selector.

Instead of this:

var line = $('.item');

Try this:

var line = $('.item[style!="display:none;"]');

using [attribute="value"] looks for elements that have attribute with value value, adding the ! before the = looks for things that do NOT match.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论