jQuery add elements to empty selection?

后端 未结 9 1392
一向
一向 2020-12-29 18:52

Why doesn\'t this work?

var spans = $();
var elem = document.getElementById(\'someId\');
spans.add(elem);

What is the proper way to start

9条回答
  •  礼貌的吻别
    2020-12-29 19:30

    What you actually want to do is use jQuery to it's full potential. You can use selectors to grab and create the collection right away. Your three lines above becomes one line:

    var spans = $('#someId');
    

    to add more ids to the collection, you can separate them with commas:

    var spans = $('#someId1, #someid2, #someid3');
    

提交回复
热议问题