jQuery: Find the element with a particular custom attribute

后端 未结 4 1829
误落风尘
误落风尘 2021-02-05 01:32

I just want to find the element with a particular value for a custom attribute.

E.g. I want to find a the div which has the attribute data-divNumber=\

相关标签:
4条回答
  • 2021-02-05 02:00

    With ES6 you can use string interpolation:

    let number = 6;
    let myDiv = $(`div[data-divNumber="${number}"]`);
    
    0 讨论(0)
  • 2021-02-05 02:03
    var number = 6;
    var myDiv = $('div[data-divNumber="' + number + '"]');
    
    0 讨论(0)
  • 2021-02-05 02:10

    You need to do some string addition to get the number into your selector and the value needs to be quoted.

    var number = 6;
    var myDiv = $('[data-divNumber="' + number + '"]');
    

    What you're trying to produce after the string addition is this result:

    $('[data-divNumber="6"]');
    
    0 讨论(0)
  • 2021-02-05 02:15

    I think what you need is:

    var number = 6;
    var myDiv = $('[data-divNumber="'+number+'"]');
    
    0 讨论(0)
提交回复
热议问题