


Summary of jQuery common operation methods and commonly used functions_jquery
An article about jQuery’s common methods and functions to keep as a memo.
JQuery common operation implementation methods
$("Tag name") //Get html elements document.getElementsByTagName("")
$("#ID") //Get a single control document.getElementById("")
$("div #ID") //Get the control in a certain control
$("#ID #ID") // Get the control through the control ID
$("label.class style name") //Get the control through class
$("#ID").val(); //Get value
$("#ID").val(""); //Assign value
$("#ID").hide(); //Hide
$("#ID").show(); //Show
$("#ID").text(); //Equivalent to taking innerText
$("#ID").text(""); //Equivalent to innerText=""
$("#ID").html(); //Equivalent to taking innerHTML
$("#ID").html(""); //Equivalent to innerHTML=""
$("#ID").css("property","value") //Add CSS style
$("form#form id").find("#the control id found").end() //Traverse the form
$("#ID").load("*.html") //Load a file
For example:
$("form#frmMain").find("#ne").css("border", "1px solid #0f0").end() // end()返回表单 .find("#chenes").css("border-top","3px dotted #00f").end() $.ajax({ url: "Result.aspx", //数据请求页面的url type:"get", //数据传递方式(get或post) dataType:"html", //期待数据返回的数据格式(例如 "xml", "html", "script",或 "json") data: "chen=h", //传递数据的参数字符串,只适合get方式 timeout:3000, //设置时间延迟请求的时间 success:function(msg)//当请求成功时触发函数 { $("#stats").text(msg); }, error:function(msg) //当请求失败时触发的函数 { $("#stats").text(msg); } }); $(document).ready(function(){}); $("#description").mouseover(function(){}); //ajax方法 $.get( "Result.aspx", //数据请求页面的url { chen: "测试",age:"25"}, //传递数据的参数字符串 function(data){ alert("Data Loaded: " + data); } //触发后的函数 ); }); }); //取得下拉选单的选取值 $(#testSelect option:selected').text(); //取文本值 或$("#testSelect").find('option:selected').text(); 或$("#testSelect").val();
------
Summary of commonly used function methods in jQuery
Event handling
ready(fn)
Code:
$(document).ready(function(){ // Your code here... });
Function: It can greatly improve the response speed of web applications. By using this method, you can call the function you bound as soon as the DOM is loaded and ready to be read and manipulated, and 99.99% of JavaScript functions need to be executed at that moment.
bind(type,[data],fn)
Code:
$("p").bind("click", function(){ alert( $(this).text() ); });
Function: Bind an event handler function to a specific event (like click) of each matching element. Plays the role of event monitoring.
toggle(fn,fn)
Code:
$("td").toggle( function () { $(this).addClass("selected"); }, function () { $(this).removeClass("selected"); } );
Function: Switch the function to be called every time you click. If a matching element is clicked, the first function specified is triggered, and when the same element is clicked again, the second function specified is triggered. It's a very interesting function that may be used when dynamically implementing certain functions.
(Events like click(), focus(), keydown() will not be mentioned here, they are commonly used in development.)
Appearance
addClass(class) and removeClass(class)
Code:
$(".stripe tr").mouseover(function(){ $(this).addClass("over");}).mouseout(function(){ $(this).removeClass("over");}) });
can also be written as:
$(".stripe tr").mouseover(function() { $(this).addClass("over") }); $(".stripe tr").mouseout(function() { $(this).removeClass("over") });
Function: Add or remove styles for specified elements to achieve dynamic style effects. In the above example, the code for moving a two-color table with the mouse is implemented.
css(name,value)
Code:
$("p").css("color","red");
Function: Very simple, it is to set the value of a style attribute in the matched element. This personal feeling is somewhat similar to addClass(class) above.
slide(),hide(),fadeIn(), fadeout(), slideUp() ,slideDown()
Code:
$("#btnShow").bind("click",function(event){ $("#divMsg").show() }); $("#btnHide").bind("click",function(evnet){ $("#divMsg").hide() });
Function: Several commonly used dynamic effect functions provided in jQuery. You can also add parameters: show(speed,[callback]) to display all matching elements in an elegant animation and optionally trigger a callback function after the display is completed.
animate(params[,duration[,easing[,callback]]])
Function: The function used to create animation effects is very powerful and can be used continuously.
Search and filter
map(callback)
HTML code:
<p><b>Values: </b></p> <form> <input type="text" name="name" value="John"/> <input type="text" name="password" value="password"/> <input type="text" name="url" value="http://www.fufuok.com/> </form>
jQuery code:
$("p").append( $("input").map(function(){ return $(this).val(); }).get().join(", ") );
结果:
[
John, password, http://www.fufuok.com/
]作用:将一组元素转换成其他数组(不论是否是元素数组)你可以用这个函数来建立一个列表,不论是值、属性还是CSS样式,或者其他特别形式。这都可以用'$.map()'来方便的建立。
find(expr)
HTML 代码:
Hello, how are you?
jQuery 代码:
$("p").find("span")
结果:
[ Hello ]
作用:搜索所有与指定表达式匹配的元素。这个函数是找出正在处理的元素的后代元素的好方法。
文档处理
attr(key,value)
HTML 代码:
jQuery 代码:
$("img").attr("src","test.jpg");
作用:取得或设置匹配元素的属性值。通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则返回 undefined 。在控制HTML标记上是必备的工具。
html()/html(val)
HTML 代码:
Hello
jQuery 代码:
$("div").html();
结果:
Hello
作用:取得或设置匹配元素的html内容,同类型的方法还有text()和val()。前者是取得所有匹配元素的内容。,后者是获得匹配元素的当前值。三者有相似的地方常用在内容的操作上。
wrap(html)
HTML 代码:
Test Paragraph.
jQuery 代码:
$("p").wrap("");
结果:
Test Paragraph.
作用:把所有匹配的元素用其他元素的结构化标记包裹起来。
这种包装对于在文档中插入额外的结构化标记最有用,而且它不会破坏原始文档的语义品质。 可以灵活的修改我们的DOM。
empty()
HTML 代码:
Hello, Person and person
jQuery 代码:
$("p").empty();
结果:
作用:删除匹配的元素集合中所有的子节点。
Ajax处理
load(url,[data],[callback])
url (String) : 待装入 HTML 网页网址。
data (Map) : (可选) 发送至服务器的 key/value 数据。
callback (Callback) : (可选) 载入成功时回调函数。
代码:
$("#feeds").load("feeds.aspx", {limit: 25}, function(){ alert("The last 25 entries in the feed have been loaded"); });
作用:载入远程 HTML 文件代码并插入至 DOM 中。这也是Jquery操作Ajax最常用最有效的方法。
serialize()
HTML 代码:
<p id="results"><b>Results: </b> </p> <form> <select name="single"> <option>Single</option> <option>Single2</option> </select> <select name="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select><br/> <input type="checkbox" name="check" value="check1"/> check1 <input type="checkbox" name="check" value="check2" checked="checked"/> check2 <input type="radio" name="radio" value="radio1" checked="checked"/> radio1 <input type="radio" name="radio" value="radio2"/> radio2 </form>
jQuery 代码:
$("#results").append( "<tt>" + $("form").serialize() + "</tt>" );
作用:序列化表格内容为字符串。用于 Ajax 请求。
工具
jQuery.each(obj,callback)
代码:
$.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n ); });//遍历数组 $.each( { name: "John", lang: "JS" }, function(i, n){ alert( "Name: " + i + ", Value: " + n );//遍历对象 });
作用:通用例遍方法,可用于例遍对象和数组。
jQuery.makeArray(obj)
HTML 代码:
<div>First</div><div>Second</div><div>Third</div><div>Fourth</div>
jQuery 代码:
var arr = jQuery.makeArray(document.getElementsByTagName("div"));
结果:
Fourth
Third
Second
First
作用:将类数组对象转换为数组对象。使我们可以在数组和对象之间灵活的转换。
jQuery.trim(str)
作用:这个大家应该很熟悉,就是去掉字符串起始和结尾的空格。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
