時(shí)間:2023-06-06 12:54:02 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-06-06 12:54:02 來(lái)源:網(wǎng)站運(yùn)營(yíng)
5.jQuery 快速網(wǎng)頁(yè)交互開(kāi)發(fā)-jQ基礎(chǔ):<script src="js/jquery-1.12.4.min.js"></script>
設(shè)置樣式:<style> * { margin: 0; padding: 0; } .box { width: 100px; height: 100px; background-color: pink; } </style></head><body> <div class="box" id="box"></div>
原生JS獲取元素:var box = document.getElementById("#box") var box = document.getElementsByTagName("div")[0]
JQ獲取元素:$(".box") $("#box")
JQ獲取 css 樣式,并設(shè)置:console.log($("#box").css("width")) // 100px $(".box").css("width",200)
事件簡(jiǎn)化:$(".box").click(function () { $(this).css("background-color","skyblue") })
運(yùn)動(dòng)方法$(".box").animate({"width": 500},1000)
<body> <p>段落1</p> <p>段落2</p> <p>段落3</p> <p>段落4</p> <p>段落5</p> <p>段落6</p> <p>段落7</p> <p>段落8</p> <script src="js/jquery-1.12.4.min.js"></script> <script> // 獲取元素 $("p").css("background-color","red") jQuery("p").css("background-color","red") </script>
<head> <style> * { margin: 0; padding: 0; } p { width: 50px; height: 50px; margin-bottom: 10px; } </style> <script src="js/jquery-1.12.4.min.js"></script></head><body> <p>段落1</p> <p>段落2</p> <p>段落3</p> <p>段落4</p></body>
$("p").css("background-color","pink") $("p").html("你好") $("p").animate({"width": 300},1000)
console.log($("p").innerHTML) // undefined $("p").style.backgroundColor = "red" // 報(bào)錯(cuò)
var ps = document.getElementsByTagName("p"); ps[0].html("haha") // 報(bào)錯(cuò)
console.log($("p"))
$().length
和$().size()
console.log($("p").length) console.log($("p").size())
var $ps = $("p") $ps[0].innerHTML = "你好"
var op = document.getElementsByTagName("p")[0] $(op).css("background-color","skyblue")
// 基礎(chǔ)選擇器 $("*") $("p") $(".box") $("#demo") // 高級(jí)選擇器 $(".box p").html("你好")
<input type="button" value="按鈕1"> <input type="button" value="按鈕2" disabled="disabled"> <input type="button" value="按鈕3"> <textarea name="" id="" cols="30" rows="10"></textarea>
input:disabled
:被禁用的按鈕input:enabled
:沒(méi)有被禁用的按鈕:input
:匹配所有 input, textarea, select 和 button 元素// 所有按鈕都編程粉色 $("input").css("backgroundColor","pink") // 只有被禁用的按鈕變藍(lán)色 $("input:disabled").css("background","blue")// 只有沒(méi)被禁用的按鈕變綠色 $("input:enabled").css("backgroundColor","green") // 所有相關(guān)的表單元素 $(":input").css("backgroundColor","yellow")
最后效果圖:$(":last") | 最后一個(gè) |
$(":eq(index)") | 下標(biāo)為 index 的項(xiàng),從0開(kāi)始 |
$(":gt(index)") | 大于下標(biāo)為 index 的項(xiàng),從0開(kāi)始 |
$(":lt(index)") | 小于下標(biāo)為 index 的項(xiàng),從0開(kāi)始 |
$(":odd") | 下標(biāo)為奇數(shù)的項(xiàng),從0開(kāi)始 |
下標(biāo)為偶數(shù)的項(xiàng),從0開(kāi)始 | |
$(":not(selector)") | 去除所有與給定選擇器匹配的元素 |
$("p:first").css("background-color","pink") $("p:last").css("background-color","skyblue") $("p:eq(5)").css("background-color","skyblue") $("p:gt(5)").css("background-color","skyblue") $("p:lt(5)").css("background-color","skyblue") $("p:odd").css("background-color","skyblue") $("p:even").css("background-color","skyblue") $("p:not(:eq(6))").css("background-color","skyblue") $("p:not(.para)").css("background-color","skyblue")
$("p").first() |
---|
$("p").last() |
$("p").eq(3) |
$("p").first().css("background-color","skyblue") $("p").last().css("background-color","skyblue") $("p").eq(4).css("background-color","skyblue")
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } p { width: 50px; height: 50px; margin-bottom: 10px; } table { border-collapse: collapse; } td { width: 100px; height: 50px; } </style></head><body> <table border="1"> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </table> <script src="js/jquery-1.12.4.min.js"></script> <script> // 原生方法 // var trs = document.getElementsByTagName("tr"); // // 遍歷 // for (var i = 0 ; i < trs.length ; i+=2) { // trs[i].style.backgroundColor = "skyblue" // } // jQuery的方法 $("tr:even").css("background-color","blue") </script></body></html>
關(guān)鍵詞:基礎(chǔ),交互
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。