jquery如果div不是id
我想将价格降低到所有div中的所有输入值,除了只有一个div sale_wrap
。 如何使用jquery进行exception处理?
jQuery的:
if($("div").attr("id") != "sale_wrap") { t_balance; } else { var sale = t_balance*((100-window.discount_p)/100); t_balance = sale; }
不使用选择器
$("div:not(#sale_wrap)")
jQuery not()
函数可以做到这一点。
要选择除了id
为sale_wrap
的div之外的所有div:
$('div').not('#sale_wrap')
然后,您可以使用each()
遍历div。
举个例子:
#HTML Some text in paragraph 1
Some text in paragraph 2
Some text in paragraph 3
#JS # turn background to blue for all 'p' except p with id=2 $('p').not('#2').each(function() { $(this).css('background-color', 'blue'); });
看看JsFiddle上的这个例子 。
尝试使用
if($('div').not('#sale_wrap')) { t_balance; } else { var sale = t_balance*((100-window.discount_p)/100); t_balance = sale; }
或者:
$('div[id != "sale_wrap"]')
只是添加更多信息。 可以像这样使用多个not selector :
$("div:not(#sale_wrap):not(.sale_class)");
要么
$("div:not(#sale_wrap, .sale_class)");
要么
$("div").not('#sale_wrap, .sale_class');