2

The querySelectorAll() method returns all elements in the document that match the specified CSS selector. Its effect is the same as that of jquery's $('') selector. The difference is that the querySelectorAll() method is a native method of Js, and there is no need to introduce the jquery.min.js library. .

jquery method

 <!DOCTYPE html>
<html>
<head>
    <title>demo</title>
    <meta charset="utf-8">
    <script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
    <div id="a">666</div>
    <script type="text/javascript">
        var a = $('#a').text();
        alert(a)
    </script>
</body>
</html>

querySelectorAll() method

 <!DOCTYPE html>
<html>
<head>
    <title>demo</title>
    <meta charset="utf-8">
</head>
<body>
    <div id="a">666</div>
    <script type="text/javascript">
        var a = document.querySelectorAll('#a');
        alert(a[0].innerText)
    </script>
</body>
</html>

Why is jquery less and less used, and even many front-end programmers start to dislike jquery, it is still related to the development of native Js, and the API of native Js to operate Dom is more and more convenient.

jQuery's Ajax operation saves us the problem of compatible browsers, and also provides a concise API to call get and post, freeing developers from tedious compatibility and using native APIs.

But now, this advantage is also very small. Whether it is the fetch API of native js or axios, they provide us with powerful Ajax usage capabilities, and axios also has the advantage of interceptors. In comparison, jquery's Ajax is really incomparable.

For Fetch Api, you can see my last article: https://segmentfault.com/a/1190000041850373


TANKING
4.8k 声望493 粉丝

热爱分享,热爱创作,热爱研究。