Home > Web Front-end > JS Tutorial > jQuery selectors, DOM operations, events, animations

jQuery selectors, DOM operations, events, animations

PHP中文网
Release: 2017-04-01 15:18:31
Original
926 people have browsed it

There are quite a few things about

jquery, so writing them down will help with future reference. This is also quite convenient.

JquerySelector

$(document).ready(function(){})
$(function(){})
Copy after login

If the obtained object is a jQuery object, then in Variables are preceded by $, For example: var $variable=jQueryObject

Selector

1, Determine whether an element exists on the page: if($(“#tt”).length>0){} Or if($(“#tt”)[0]){};

2, Basic Selector

whose <🎜>id<🎜> is <🎜>test<🎜>

$(“#test”) 选择idtest的元素

$(“.test”)选择classtest的所有元素

$(“p”)选择所有的

标签

$(“*”)选择页面上的所有元素

$(“span ,#two”)选择页面上所有的标签和idtwo的元素

$("#test") <🎜>Select the element <🎜><🎜>
<🎜>$(".test")<🎜>Select<🎜>All elements <🎜><🎜>< of class<🎜><🎜> for <🎜>test<🎜> /td>
<🎜>$("p")<🎜> Select all <🎜>

<🎜>tags<🎜><🎜>

<🎜>$("*")<🎜>Select all elements on the page<🎜><🎜>
<🎜>$(“span ,#two”)<🎜>Select all <🎜>< on the page The element <🎜><🎜>
with the 🎜> tag and <🎜>id<🎜> is <🎜>two<🎜>

3, 层次选择器

$(“p span”)表示选取

$(“p span”)表示选取p中的所有的span元素

$(“parent>children”)选取parent下所有children的子元素

$(‘.one span')选取classone的下一个span元素

$(‘.one~p')选取classone的后面的所有的p兄弟元素

p中的所有的<🎜>span元素<🎜><🎜>

$(“parent>children”)选取<🎜>parent下所有<🎜>children的子元素<🎜><🎜>

$(‘.one span')选取<🎜>class为<🎜>one的下一个<🎜>span元素<🎜><🎜>

$(‘.one~p')选取<🎜>class为<🎜>one的后面的所有的<🎜>p兄弟元素<🎜><🎜>

$('.one span') is equivalent to $(".one").next("span")

$('one~p')Equivalent to $(“.one”).nextAll(“p”)

$(“.one”).siblings(“p”) Select all sibling elements p of class as one, regardless of

4, Filter Selector

(1)BasicFilter

$( "input:even")Select the index$ ("input:odd")Select odd-numbered input$("input:eq (1)")Select the inputinput element whose index is greater than 1< that is executing the animation

$(“p:first”)Select all pThe first p element in the element

$(“p:last”)Select allpThe lastpelement

$(“input:not(.myClass)”)Select the input element

that is an even number

input element

elements at index

element

$( "input:gt(1)")

Selects the
(greater than

1 does not include 1)

$("input:lt(1)")Select the input element<🎜 whose index is less than 1 > (less than 1 excluding 1)

$(":

header”)Select all h1, h2...

$( "p:animated")

Select the p element

🎜>(

2)Content Filter

$("p:contains('

I

$(“p:contains(‘')”)选取含有文字“我”的p元素

$(“p:empty”)选取不包含子元素(包含文本元素)的p空元素

$(“p:has(p)”)选取含有p元素的p元素

$(“p:parent”)选取拥有子元素(包含文本元素)的p元素

')")<🎜>Select < that contains the text "I" 🎜>p<🎜>Element<🎜><🎜><🎜><🎜><🎜><🎜><🎜>$(“p:empty”)<🎜>Selects elements that do not contain child elements (including text elements) <🎜>p<🎜>Empty element<🎜><🎜><🎜><🎜><🎜><🎜><🎜>$(“p:has(p)”)<🎜>Select containing <🎜>p The <🎜>p<🎜> element of the <🎜> element <🎜><🎜><🎜><🎜><🎜><🎜><🎜>$(“p:parent”)<🎜> selects the owning child element ( <🎜>p<🎜> element <🎜><🎜><🎜><🎜><🎜><🎜> containing a text element)

(3)Visibility filter selector

$(“:hidden”)

$(“:hidden”)选取所有不可见的元素。包括,

等元素。如果只想选取元素,可以使用$(“input:hidden”)

$(“p:visible”)选取所有可见的

元素

Select all invisible elements. Including <🎜>,

<🎜> and <🎜>

< 🎜> and other elements. If you only want to select the <🎜><🎜> element, you can use <🎜>$("input:hidden")<🎜>

$("p:visible")<🎜>Select all visible <🎜>

<🎜> elements <🎜><🎜>

 4属性过滤选择器

$(“p[id]”)

$(“p[id]”)选取拥有属性id的元素

$(“p[title=test]”)选取属性titletestp元素

$(“p[title!=test]”)选取属性title不是testp元素(没有属性titlep也会被选取)

$(“p[title^=test]”)选取属性titletest开始的p元素

$(“p[title$=test]”)选取属性titletest结束的p元素

$(“p[title*=test]”)选取属性title包含testp元素

$(“p[id][title$='test']”)选取拥有属性id,并且属性titletest结束的p元素

选取拥有属性<🎜>id<🎜>的元素<🎜><🎜>

$(“p[title=test]”)<🎜>选取属性<🎜>title<🎜>为<🎜>test<🎜>的<🎜>p<🎜>元素<🎜><🎜>

$(“p[title!=test]”)<🎜>选取属性<🎜>title<🎜>不是<🎜>test<🎜>的<🎜>p<🎜>元素(没有属性<🎜>title<🎜>的<🎜>p<🎜>也会被选取)<🎜><🎜>

$(“p[title^=test]”)<🎜>选取属性<🎜>title<🎜>以<🎜>test<🎜>开始的<🎜>p<🎜>元素<🎜><🎜>

$(“p[title$=test]”)<🎜>选取属性<🎜>title<🎜>以<🎜>test<🎜>结束的<🎜>p<🎜>元素<🎜><🎜>

$(“p[title*=test]”)<🎜>选取属性<🎜>title<🎜>包含<🎜>test<🎜>的<🎜>p<🎜>元素<🎜><🎜>

$(“p[id][title$='test']”)<🎜>选取拥有属性<🎜>id<🎜>,并且属性<🎜>title<🎜>以<🎜>test<🎜>结束的<🎜>p<🎜>元素<🎜><🎜>

(5)Child element filter selector

:eq(index)

:eq(index)只匹配一个元素,而:nth-child将为每一个父元素匹配子元素,并且:nth-child(index)index是从1开始的,而:eq(index)是从0算起的

:first只返回单个元素,而:first-child选择符将为每个父元素匹配第一个子元素。

例如$(“ul li:first-child”)选取每个ul中第一个li元素

:last只返回单个元素,而:last-child选择符将为每个父元素匹配最后一个子元素

$(“ul li:only-child”)ul中选取是唯一子元素的li元素

will only match one element, while <🎜>:nth-child<🎜> will match every parent element Match child elements, and the <🎜>index<🎜> of <🎜>:nth-child(index)<🎜> starts from <🎜>1<🎜>, and <🎜>:eq(index)<🎜> It is calculated from <🎜>0<🎜><🎜><🎜>

:first<🎜> only returns a single element, while <🎜>:first-child<🎜> selector will match the first child element of each parent element. <🎜><🎜>

<🎜>For example, <🎜>$("ul li:first-child")<🎜>selects the first <🎜>li<🎜 in each <🎜>ul<🎜> >Element<🎜><🎜>

:last<🎜> only returns a single element, while the <🎜>:last-child<🎜> selector will match the last child element <🎜><🎜>

$("ul li:only-child")<🎜>在<🎜> Select the <🎜>li<🎜> element that is the only child element in ul<🎜><🎜><🎜>

The

:nth-child() selector is a very commonly used child element filtering selector. The detailed functions are as follows:

1.:nth-child(even) can Select elements whose index value under each parent element is an even number

2. :nth-child(odd)can select elements whose index value under each parent element is an odd number

3.:nth-child(2)can select the element whose index value is equal to 2 under each parent element

4.nth -child(3n) can select elements (n starting from 0 whose index value under each parent element is a multiple of 3 )

5.nth-child(3n 1) can select the element (n whose index value under each parent element is (3n 1) Starting from 0)

(6) Form object attribute filter selector

< table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 18pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" border="1">

$("#form1 :enabled ”)

$(“#form1 :enabled”)选取idform1的表单内的所有可用元素

$(“#form2 :disabled”)选取id为“form2”的表单内的所有不可用元素

$(“input:checked”)选取所有被选中的input元素

$(“select :selected”.text())选取所有被选中的选项元素

Select all available elements in the form with <🎜>id<🎜> being <🎜>form1<🎜><🎜><🎜>

$("#form2 :disabled")<🎜>Select<🎜>id<🎜> as "<🎜 >form2<🎜>” All unavailable elements in the form<🎜><🎜>

$("input:checked")<🎜>Select all selected <🎜>input<🎜> elements<🎜><🎜>

$("select :selected".text())<🎜>Select All selected option elements<🎜><🎜>

5.表单选择器

$(“:input”)选取所有inputtextareaselectbutton元素

$(“:text”)选取所有的单行文本框

$(“:password”)选取所有的密码框

$(“:radio”)选取所有的单选框

$(“:checkbox”)选取所有的复选框

$(“:submit”)选取所有的提交按钮

$(“:image”)选取所有的图像按钮

$(“:reset”)选取所有的重置按钮

$(“:button”)选取所有的按钮

$(“:file”)选取所有的上传

$(“:hidden”)选取所有不可见元素

$(“:input”)选取所有<🎜>input、<🎜>textarea、<🎜>select和<🎜>button元素<🎜><🎜>
<🎜>$(“:text”)选取所有的单行文本框<🎜><🎜>
<🎜>$(“:password”)选取所有的密码框<🎜><🎜>
<🎜>$(“:radio”)选取所有的单选框<🎜><🎜>
<🎜>$(“:checkbox”)选取所有的复选框<🎜><🎜>
<🎜>$(“:submit”)选取所有的提交按钮<🎜><🎜>
<🎜>$(“:image”)选取所有的图像按钮<🎜><🎜>
<🎜>$(“:reset”)选取所有的重置按钮<🎜><🎜>
<🎜>$(“:button”)选取所有的按钮<🎜><🎜>
<🎜>$(“:file”)选取所有的上传域<🎜><🎜>
<🎜>$(“:hidden”)选取所有不可见元素<🎜><🎜>

DomOperation

Dom core(core), HTML-DOM and CSS-DOM

1. Method to insert nodes

append()

$(“p”) .append(“You”)

test You

appendTo()

$(“You”).appendTo(“p” )

test you

prepend()

$(“p”). prepend ( “ You”)

Youtest

prependTo()

$(“p”). prependTo ( “you”)

Youtest

After()

$(“p”). after ( “”)

test

insertAfter()

$( “”). insertAfter (“p”)

test

Before()

$(“p”). before ( “”)

test

insertBefore()

$( “”). insertBefore (“p”)

test

2. 删除节点

Remove()方法 empty()清空节点

3. 复制节点

Clone()           $(this).clone(true).appendTo(“body”)
Copy after login

复制元素的同时复制元素中所绑定的事件

4. 替换节点

replaceWith():$(“p”).replaceWith(“<a>test</a>”);
replaceAll():$(“<a>test</a>”). replaceAll (“p”);
Copy after login

5. 包裹节点

Wrap()

$(“strong”).wrap(“”);ssss

wrapAll()

$(“strong”). wrapAll (“”);ssss

ssss

wrapInner()

$(“strong”).wrapInner (”);ssssg>

6. 属性操作

Attr();    设置多个$(“p”).attr({“title”:”dd”,”name”:”myp”})
Copy after login

removeAttr()移除属性

7. 样式操作

获取和设置样式

Attr()

追加样式

addClass()

移除样式

removeClass() removeClass(“one two”)

切换样式

toggle()主要控制行为上的重复切换

toggleClass()样式上的重复切换

判断是否含有某个样式

hasClass()等价于is(“.one”)

8.设置和获取html、文本和值

html()

读取或者设置某个元素中的HTML内容

text()

读取或者设置某个元素中文本内容

val()

设置和获取元素的值defaultValue初始值

html()<🎜><🎜>
<🎜>读取或者设置某个元素中的<🎜>HTML<🎜>内容<🎜><🎜>
<🎜>text()<🎜><🎜><🎜>读取或者设置某个元素中文本内容<🎜><🎜>
<🎜>val()<🎜><🎜><🎜>设置和获取元素的值<🎜>defaultValue<🎜>初始值<🎜><🎜>

9.遍历节点

Children()

取得匹配元素的子元素的集合,只考虑子元素不考虑后代元素

Next()

取得匹配元素后面紧邻的同辈元素

Prev()

取得匹配元素前面紧邻的同辈元素

Siblings()

取得匹配元素前后所有的同辈元素

Closest()

取得最近的匹配元素

还有很多遍历方法:find(),filter(),nextAll(),prevAll(),parent(),parents()

Children()<🎜><🎜>
<🎜>取得匹配元素的子元素的集合,只考虑子元素不考虑后代元素<🎜><🎜>
<🎜>Next()<🎜><🎜><🎜>取得匹配元素后面紧邻的同辈元素<🎜><🎜>
<🎜>Prev()<🎜><🎜><🎜>取得匹配元素前面紧邻的同辈元素<🎜><🎜>
<🎜>Siblings()<🎜><🎜><🎜>取得匹配元素前后所有的同辈元素<🎜><🎜>
<🎜>Closest()<🎜><🎜><🎜>取得最近的匹配元素<🎜><🎜>
<🎜>还有很多遍历方法:<🎜>find(),filter(),nextAll(),prevAll(),parent(),parents()<🎜>等<🎜><🎜>

CSS-DOMOperations

Get style

$(“.one”).css(“color”)

Set style

$(“.one”).css(“color”, "red")

$(".one").css({"opacity":"0.5","background-color":"blue" })

Height()

$(" .one").height(),Settings: $(".one").height("20px")

Width()

$(“.one”).width(), Settings: $(“.one”).width(“200px”)

Offset()

Get the relative offset of the element in the current window, including top and left

Position()

Get the element relative to the nearest position style attribute set to relative or The relative offset of the grandparent node of absolute. The object it returns also includes two attributes, namely top and left

ScrollTop()

Get and Set the distance between the element's scroll bar and the top

ScrollLeft()

Gets and sets the distance between the scroll bar of the element and the left side

事件和动画

事件

(1) 绑定事件

bind(type[,data],fn);

参数:type:事件类型,也可以自定义名称

data:作为event.data属性值传递给事件对象的额外数据对象

fn:用来绑定的处理函数

绑定多个事件类型

Ex:$(“p”).bind(“mouseover mouseout”,function(){
$(this).toggleClass(“over”);
})
Copy after login

(2) 合成事件

Hover(enter,leave);

用于模拟光标悬停事件。当光标移动到元素上时,会触发指定的第一个函数(enter);当光标移除这个元素时,会触发指定的第二个函数(leave

toggle(fn1,fn2,….fnN);

用户模拟鼠标连续单击事件。第一次单击元素,触发指定的第一个函数;当在次单击同一个元素时,则触发指定的第二个函数(fn2);如果有更多的函数,则依次触发,知道最后一个。

(3) 冒泡事件

假设网页上有2个元素,其中一个元素嵌套在另一个元素里,并且都被绑定了click事件,同时body元素也绑定了click事件,当点击最里面元素的事件时,同时其它事件也会触发。从内向外

停止事件冒泡的方法:

stopPropagation()
Ex:$(‘span&#39;).bind(“click”,function(event){
Var txt=$().html()+”<p>内层span元素被单击</p>”;
$(‘#msg&#39;).html(txt);
Event.stopPropagation(); 停止事件冒泡
})
Copy after login


停止表单默认提交:event.preventDefault()==return false;

(4) 移除事件

Unbind([type][,data]);
Copy after login

Type:事件类型

Data:将要移除的函数

(5) 触发一次函数

One(type,[data],fn);
Copy after login

事件触发后立即解除

动画

Show()

Slow: 600ms, normal: 400 milliseconds, fast: 200milliseconds

Hide()

Fadeln()

Reduces the opacity of an element for a specified period of time until the element completely disappears

fadeout()

The opposite of the above

slideUp()

Contrary to the following

slideDown()

The element will be displayed extending from top to bottom

Custom animationanimate

Grammar structure: animate(params, speed, callback);

The parameter description is as follows:

1. Params: A mapping containing style attributes and values, such as {property1: "value1", property2: "value2",…}

2. Speed:Speed ​​parameter, optional.

Callback: Function executed when the animation is completed, optional

Just add this for now Click it and improve it slowly in the future!

The above is the content of jQuery selectors, DOM operations, events, and animations. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template