2016-08-16 75 views
0

起源脚本,做工精细,如果我只添加一个类:如何在Javascript中添加这个条件后添加更多的类?

function check_btn_charge() { 
     if (parseInt(jQuery(".total-change-price").text()) >= 0) { 
      jQuery(".btn-action-save-charge"+" "+"btn-danger").prop("disabled", true); 
     } 
     else { 
      jQuery(".btn-action-save-charge"+" "+"btn-danger").prop("disabled", false); 
     } 
    } 

而且我一直在努力,在使用加(+)号和空格字符作为单独的类此条件下增加更多的类,像下面的脚本,但不工作:

function check_btn_charge() { 
     if (parseInt(jQuery(".total-change-price").text()) >= 0) { 
      jQuery(".btn-action-save-charge").prop("disabled", true); 
     } 
     else { 
      jQuery(".btn-action-save-charge").prop("disabled", false); 
     } 
    } 

我该怎么做这个脚本,以便工作好?

+0

你的代码混合。而' '不是空格 - 它是字符串'&nbsp'。如果你想要空间 - 这里是'''''' –

+0

你能解释我的脚本中的详细内容吗? 是这样的代码吗? ''jQuery(“。btn-action-save-charge”+''+“btn-danger”)。prop(“disabled”,false);' – Ferdian

+0

用逗号分隔不同的类/选择符,就像在CSS中一样...'btn-danger'缺少一个前导点'''它应该是'jQuery(“.btn-active-save-charge,.btn-danger”)' – Aziz

回答

2

如果你要选择与这两类“MyClass1的”和“myClass2”的元素:

jQuery('.myClass1.myClass2') 

如果你想与阶级“MyClass1的”或“myClass2”的一个选择元素:

jQuery('.myClass1, .myClass2') 

超过两个类遵循相同的规则。

More info

+0

非常感谢你@Mojtaba,你的建议是帮助我:) – Ferdian

+0

@Ferdian,很高兴听到这一点。 – Mojtaba

0

这里有一个纯JavaScript相当于为那些不喜欢的jQuery:

var and = document.querySelectorAll(".one.two"); 
var or = document.querySelectorAll(".one, .two"); 
var xor = document.querySelectorAll(".one:not(.two), .two:not(.one)"); 
var nor = document.querySelectorAll("button:not(.one):not(.two)"); 

See it in action on JSFiddle