Jquery基础知识;

时间:2023-03-08 21:32:19

1.jquery语法

jQuery 语法是通过选取 HTML 元素,并对选取的元素执行某些操作。

基础语法: $(selector).action()

美元符号定义 jQuery

选择符(selector)"查询"和"查找" HTML 元素

jQuery 的 action() 执行对元素的操作

2.jquery选择器(标签选择器,id选择器,class选择器)

<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
</head> <body>
<p>1111111</p>
<div id="aa" style="border:1px solid red; width:20px; height:20px;"></div>
<div class="aa" style="border:1px solid red; width:20px; height:20px;"></div> </body>
</html>
<script> $(document).ready(function(){
$("#aa").click(function(){ //id选择器
$("p").hide() //点击id是aa的元素,隐藏p标签 })
});
$(document).ready(function(){
$(".aa").click(function(){ //class选择器
$("p").hide() })
}); </script>

补充:

$("*") 选取所有元素
$(this) 选取当前 HTML 元素
$("p.intro") 选取 class 为 intro 的 <p> 元素
$("p:first") 选取第一个 <p> 元素
$("ul li:first") 选取第一个 <ul> 元素的第一个 <li> 元素
$("ul li:first-child") 选取每个 <ul> 元素的第一个 <li> 元素
$("[href]") 选取带有 href 属性的元素
$("a[target='_blank']") 选取所有 target 属性值等于 "_blank" 的 <a> 元素
$("a[target!='_blank']") 选取所有 target 属性值不等于 "_blank" 的 <a> 元素
$(":button") 选取所有 type="button" 的 <input> 元素 和 <button> 元素
$("tr:even") 选取偶数位置的 <tr> 元素
$("tr:odd") 选取奇数位置的 <tr> 元素

3.常用的 jQuery 事件方法

$(document).ready()

$(document).ready() 方法允许我们在文档完全加载完后执行函数。

click()

click() 方法是当按钮点击事件被触发时会调用一个函数。

dblclick()

当双击元素时,会发生 dblclick 事件。

dblclick() 方法触发 dblclick 事件,或规定当发生 dblclick 事件时运行的函数

<body>
<p>1111111</p> </body>
</html>
<script> $(document).ready(function(){
$("p").dblclick(function(){ //双击p标签,内容会消失
$(this).hide() //this代表点击的此内容 })
}); </script>

mouseenter()

当鼠标指针穿过元素时,会发生 mouseenter 事件。

mouseenter() 方法触发 mouseenter 事件,或规定当发生 mouseenter 事件时运行的函数

<p>1111111</p>

</body>
</html>
<script> $(document).ready(function(){
$("p").mouseenter(function(){ //双击p标签,内容会消失
$(this).hide() //this代表点击的此内容 })
}); </script>

mouseleave()

当鼠标指针离开元素时,会发生 mouseleave 事件。

mouseleave() 方法触发 mouseleave 事件,或规定当发生 mouseleave 事件时运行的函数

<p>1111111</p>

</body>
</html>
<script> $(document).ready(function(){
$("p").mouseleave(function(){ //双击p标签,内容会消失
$(this).hide() //this代表点击的此内容 })
}); </script>

mousedown()

当鼠标指针移动到元素上方,并按下鼠标按键时,会发生 mousedown 事件。

mousedown() 方法触发 mousedown 事件,或规定当发生 mousedown 事件时运行的函数

mouseup()

当在元素上松开鼠标按钮时,会发生 mouseup 事件。

mouseup() 方法触发 mouseup 事件,或规定当发生 mouseup 事件时运行的函数

hover()

hover()方法用于模拟光标悬停事件。

当鼠标移动到元素上时,会触发指定的第一个函数(mouseenter);当鼠标移出这个元素时,会触发指定的第二个函数(mouseleave)

<p>1111111</p>

</body>
</html>
<script> $(document).ready(function(){
$("p").hover(
function(){alert("come")},
function(){alert("go")}) //this代表点击的此内容
}); </script>

Jquery基础知识;

focus()

当元素获得焦点时,发生 focus 事件。

当通过鼠标点击选中元素或通过 tab 键定位到元素时,该元素就会获得焦点。

focus() 方法触发 focus 事件,或规定当发生 focus 事件时运行的函数

blur()

当元素失去焦点时,发生 blur 事件。

blur() 方法触发 blur 事件,或规定当发生 blur 事件时运行的函数

<body>
姓名:<input type="text" ><br>
邮箱:<input type="text"> </body>
</html>
<script>
$(document).ready(function() {
$("input").focus(function(){ //获取焦点时,要运行的函数
$(this).css("background-color","red")
}); $("input").blur(function(){ //失去焦点时,要运行的函数 $(this).css("background-color","white") }); }); </script>

Jquery基础知识;

常见 DOM 事件:

鼠标事件 键盘事件 表单事件 文档/窗口事件
click keypress submit load
dblclick keydown change resize
mouseenter keyup focus scroll
mouseleave   blur unload