Performance of jQuery Selectors with ID

后端 未结 9 2014
有刺的猬
有刺的猬 2021-01-12 10:52

I know in jQuery if we use ID to select elements,it\'s so efficient.I have a question about this selectors:

please consider this 3 selectors:

         


        
9条回答
  •  时光说笑
    2021-01-12 11:45

    this is the way to stop times between some javascript calls

    selectorTimes = [];
    var start = new Date().getTime();
    $('#MyElement')
    selectorTimes.push(new Date().getTime()-start);
    
    start = new Date().getTime()
    $('#Mytbl #MyElement')
    selectorTimes.push(new Date().getTime()-start);
    
    start = new Date().getTime()
    $('#Mytbl .MyClass')
    selectorTimes.push(new Date().getTime()-start);
    
    console.log(selectorTimes);
    

    i think the second selector is not efficient, if you have a domid select this directly: $('#MyElement')

提交回复
热议问题