[removed] querySelector Null vs querySelector

后端 未结 4 1657
既然无缘
既然无缘 2020-12-18 21:41

What is the main difference between these two methods of referencing?

What are the benefits of using one or the other? Also what kind of usage-case would they each

4条回答
  •  误落风尘
    2020-12-18 22:33

    You can try to avoid the conditional statement with:

    var selection = document.querySelectorAll('.selector');
    selection.forEach(function(item) {
        alert(item);
    });
    

    Caution! querySelectorAll() behaves differently than most common JavaScript DOM libraries, which might lead to unexpected results

    Source: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

提交回复
热议问题