jQuery how to find an element based on a data-attribute value?

后端 未结 9 1521
鱼传尺愫
鱼传尺愫 2020-11-22 01:02

I\'ve got the following scenario:

var el = \'li\';

and there are 5

  • \'s on the page each with a data-slide=numb
  • 相关标签:
    9条回答
    • 2020-11-22 01:30

      Going back to his original question, about how to make this work without knowing the element type in advance, the following does this:

      $(ContainerNode).find(el.nodeName + "[data-slide='" + current + "']");
      
      0 讨论(0)
    • 2020-11-22 01:34

      Without JQuery, ES6

      document.querySelectorAll(`[data-slide='${current}']`);
      

      I know the question is about JQuery, but readers may want a pure JS method.

      0 讨论(0)
    • 2020-11-22 01:36

      You have to inject the value of current into an Attribute Equals selector:

      $("ul").find(`[data-slide='${current}']`)
      

      For older JavaScript environments (ES5 and earlier):

      $("ul").find("[data-slide='" + current + "']"); 
      
      0 讨论(0)
    提交回复
    热议问题