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

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

Given the following XML:


    Max
    Jen
    &l         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-31 17:30

    Try this:

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

    Basically you chain the sex and state attributes together in a single simple selector (this would be your logical AND), and repeat them once per state (and this would be your logical OR).

    Test:

    $(xml).find("user[sex='m'][state='CA'], user[sex='m'][state='NV']")
          .each(function() {
              alert($(this).text() + " - " + $(this).attr('state'));
          });
    

    Output:

    Max - CA
    Bob - NV
    Jon - CA
    

提交回复
热议问题