一、jQuery 的 dom 操作
<ul class="list">
<li class="item">item1</li>
<li class="item">item2</li>
<li class="item">item3</li>
</ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
let li = document.createElement("li");
li.textContent = "item4";
li.style.color = "red";
li.classList.add("item");
document.querySelector(".list").append(li);
$(".list").append("<li>item5</li>").find(":last").addClass("item").css("color", "green");
$("<li>item6</li>").addClass("item").css("color", "blue").appendTo(".list");
$(".list").prepend("<li>item0</li>").find(":first").addClass("item").css("color", "red");
$("<li>item-1</li>").addClass("item").css("color", "skyblue").prependTo(".list");
$(".list .item").eq(3).after('<li class="item">new li-1</li>').next().css("color", "violet");
$(".list .item").eq(3).before('<li class="item">new li-2</li>').prev().css("color", "green");
$("<h3>商品列表</h3>").insertBefore(".list");
$("<p>总计: 5万元</p>").insertAfter(".list");
$("h3").replaceWith("<p>购物车</p>");
$("<em>购物清单</em>").replaceAll("p:first-of-type");
$(".list .item").last().remove();
$(".list .item").remove(".item:nth-of-type(3)");
$(".list .item:last").clone().prependTo(".list");
二、jQuery 的表单事件
<style>
body {
background-color: lightcyan;
}
form {
background-color: #fff;
display: grid;
width: 15em;
gap: 1em;
box-shadow: 0 0 10px #888;
padding: 1em;
margin: 2em auto;
}
form input {
border: none;
border-bottom: 1px solid;
outline: none;
}
form button:hover {
cursor: pointer;
font-weight: bolder;
}
</style>
<form action="check.php">
<label>LOGIN:</label>
<input type="text" name="username" placeholder="UserName" autofocus />
<input type="password" name="password" placeholder="Password" />
<button>Submit</button>
</form>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
$("form").submit(ev => ev.preventDefault());
const user = $('input[name="username"]');
user.blur(function () {
let tips = "";
const users = ["admin", "peter", "zhu"];
if ($(this).val().length === 0) {
tips = "用户名不能为空";
$(this).focus();
} else if (users.indexOf($(this).val()) === -1) {
tips = `用户名不存在, <a href="register.php" style="text-decoration:none">请注册</a>`;
} else {
tips = `<i style="color:green">用户名正确</i>`;
}
if ($(this).next()[0].tagName !== "SPAN") {
$("<span></span>")
.html(tips)
.css({
color: "red",
fontSize: "12px",
})
.insertAfter($(this));
}
});
user.on("input", function () {
if ($(this).next()[0].tagName === "SPAN") $(this).next().remove();
});
</script>
三、jQuery-Ajax
1 $.get(url, data, callback)
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<button class="get"> $.get(): 请求数据</button>
<script>
$(".get").click((ev) => {
$.get("users.php", { id: 2 }, (data) => {
$(ev.target).after("<div></div>").next().html(data);
});
});
</script>
2 $.post(url, data, callback)
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<button class="post"> $.post(): 请求数据</button>
<script>
$(".post").click(ev => {
$.post("users.php", { id: 3 }, data => {
$(ev.target).after("<div></div>").next().html(data);
});
});
</script>
3 $.ajax(url, data, callback)
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<button class="get">$.get(): 请求数据</button>
<button class="post">$.post(): 请求数据</button>
<script>
$.ajax({
type: "get",
url: "users.php",
data: { id: 2 },
dataType: "html",
success: (data) => console.log(data),
});
$(".post").click(() => {
$.ajax({
type: "post",
url: "users.php",
data: { id: 2 },
dataType: "text",
success: (data) => console.log(data),
});
});
</script>
4 jsonp 跨域请求
$(".jsonp").click(ev => {
$.ajax({
type: "get",
url: "http://world.io/test.php?id=2&callback=?",
dataType: "jsonp",
success: function (data) {
console.log(data);
let user = `${data.name} : ( ${data.email})`;
$("button:last-of-type").after("<div></div>").next().html(user);
},
});
});
function show(data) {
console.log(data);
let user = `${data.name} : ( ${data.email})`;
$("button:last-of-type").after("<div></div>").next().html(user);
}