How do I combine logical OR with logical AND within a jQuery attribute selector?

前端 未结 2 1652
醉酒成梦
醉酒成梦 2020-12-31 17:23

Given the following XML:


    Max
    Jen
    &l         


        
2条回答
  •  臣服心动
    2020-12-31 17:52

    You can combine multiple selectors, but your syntax isn't exactly correct:

    $(xml).find("user[state='CA'],[state='NV']")
    

    This finds all elements with the state "CA" and any other nodes (not necessarily ) with state "NV". What you want is:

    $(xml).find("user[state=CA][sex=m],user[state=NV][sex=m]")
    

    If you don't want to repeat yourself:

    $(xml).find("user").filter("[state='CA'],[state='NV']").filter("[sex='m']");
    

提交回复
热议问题