1、原生js与jQuery的比较
- 原生的js:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>标题</title>
<style>
div{
height: 100px;
margin-top: 10px;
}
</style>
</head>
<body>
<input type="button" value="设置边框" id="btnOne"/>
<input type="button" value="设置文本" id="btnTwo"/>
<div></div>
<div></div>
<div></div>
</body>
</html>
<!--需求:点击按钮给三个div设置边框和文本-->
<script>
window.onload = function (){
let btnOne = document.getElementById("btnOne")
var btnTwo = document.getElementById("btnTwo")
var divs = document.getElementsByTagName("div")
btnOne.onclick = function (){
for (var i = 0; i<divs.length;i++){
divs[i].style.border = '1px solid red'
}
}
btnTwo.onclick = function (){
for (var i =0;i<divs.length;i++){
divs[i].textContent = "我是设置的文本";
}
}
}
</script>
- jQuery代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>标题</title>
<style>
div{
height: 100px;
margin-top: 10px;
}
</style>
</head>
<body>
<input type="button" value="设置边框" id="btnOne"/>
<input type="button" value="设置文本" id="btnTwo"/>
<div></div>
<div></div>
<div></div>
</body>
</html>
<!--需求:点击按钮给三个div设置边框和文本-->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function (){
$('#btnOne').click(function (){
$('div').css('border','1px solid red');
});
$('#btnTwo').click(function (){
$('div').text('我是文本!!!!!');
});
});
$(document).ready(function (){
console.log("我又是一个入口函数")
});
</script>