Home Web Front-end JS Tutorial Summary of jQuery common operation implementation methods and common function methods_jquery

Summary of jQuery common operation implementation methods and common function methods_jquery

May 16, 2016 pm 06:07 PM
Commonly used functions Common operations

jQuery 常见操作实现方式

$("标签名") //取html元素 document.getElementsByTagName("")

$("#ID") //取单个控件document.getElementById("")

$("div #ID") //取某个控件中 控件

$("#ID #ID") // 通过控件ID取其中的控件

$("标签.class样式名") //通过class来取控件

$("#ID").val(); //取value值

$("#ID").val(""); //赋值

$("#ID").hide(); //隐藏

$("#ID").show(); //显示

$("#ID").text(); //相当于取innerText

$("#ID").text(""); //相当于innerText=""

$("#ID").html(); //相当于取innerHTML

$("#ID").html(""); //相当于innerHTML=""

$("#ID").css("属性","值") //添加CSS样式

$("form#表单id").find("#所找控件id").end() //遍历表单

$("#ID").load("*.html") //载入一个文件

例如:

$("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();

jQuery中常用的函数方法总结

事件处理

ready(fn)

代码:
$(document).ready(function(){
  // Your code here...
});

作用:它可以极大地提高web应用程序的响应速度。通过使用这个方法,可以在DOM载入就绪能够读取并操纵时立即调用你所绑定的函数,而99.99%的JavaScript函数都需要在那一刻执行。

bind(type,[data],fn)

代码:
$("p").bind("click", function(){
  alert( $(this).text() );
});

作用:为每一个匹配元素的特定事件(像click)绑定一个事件处理器函数。起到事件监听的作用。

toggle(fn,fn)
代码:

$("td").toggle(
  function () {
    $(this).addClass("selected");
  },
  function () {
    $(this).removeClass("selected");
  }
);

作用:每次点击时切换要调用的函数。如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数。挺有趣的一个函数,在动态实现某些功能的时候可能会用到。

(像click(),focus(),keydown()这样的事件这里就不提了,那些都是开发中比较常用到的。)

外观效果

addClass(class)和removeClass(class)

代码:
$(".stripe tr").mouseover(function(){
               $(this).addClass("over");}).mouseout(function(){
               $(this).removeClass("over");})
});
也可以写成:
$(".stripe tr").mouseover(function() { $(this).addClass("over") });
$(".stripe tr").mouseout(function() { $(this).removeClass("over") });

作用:为指定的元素添加或移除样式,从而实现动态的样式效果,上面的实例中实现鼠标移动双色表格的代码。

css(name,value)

代码:
$("p").css("color","red");

作用:很简单,就是在匹配的元素中,设置一个样式属性的值。这个个人感觉和上面的addClass(class)有点类似。

slide(),hide(),fadeIn(), fadeout(), slideUp() ,slideDown()

代码:

$("#btnShow").bind("click",function(event){ $("#divMsg").show() });
$("#btnHide").bind("click",function(evnet){ $("#divMsg").hide() });

作用:jQuery中提供的比较常用的几个动态效果的函数。还可以添加参数:show(speed,[callback])以优雅的动画显示所有匹配的元素,并在显示完成后可选地触发一个回调函数。

animate(params[,duration[,easing[,callback]]])

作用:制作动画效果用到的函数,功能非常的强大,可以连续使用此函数。

查找筛选

map(callback)
HTML 代码:

Values:



 
 
    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 代码:

Results:



 
 

  check1
  checked="checked"/> check2
  checked="checked"/> radio1
  radio2

jQuery 代码:
$("#results").append( "" + $("form").serialize() + "" );

作用:序列化表格内容为字符串。用于 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 代码:

First
Second
Third
Fourth

jQuery code:
var arr = jQuery.makeArray(document.getElementsByTagName("div"));
Result:
Fourth
Third
Second
First

Function: Convert array-like objects into array objects. Allows us to flexibly convert between arrays and objects.

jQuery.trim(str)
Function: This should be familiar to everyone, it is to remove the spaces at the beginning and end of the string.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Quick start guide to commonly used functions in the pandas library Quick start guide to commonly used functions in the pandas library Jan 24, 2024 am 08:05 AM

The pandas library is a commonly used data processing and analysis tool in Python. It provides a wealth of functions and methods that can easily complete data import, cleaning, processing, analysis and visualization. This article will introduce a quick start guide to commonly used functions in the pandas library, with specific code examples. The data import pandas library can easily import data files in various formats through functions such as read_csv and read_excel. Here is a sample code: importpandas

What are the common GitHub operations in PHP programming? What are the common GitHub operations in PHP programming? Jun 12, 2023 am 11:43 AM

As a programming language widely used in Internet application development, PHP has become one of the most popular programming languages ​​in the world. As a very influential code hosting platform, GitHub has also attracted the attention of more and more PHP programmers. In the process of using GitHub for PHP programming, there are some common operations and techniques that need to be mastered. The following article will introduce some key GitHub operations to help PHP programmers better use GitHub for development. Create a repository in Git

Comprehensive list of commonly used functions in the Numpy library: quick start and practice guide Comprehensive list of commonly used functions in the Numpy library: quick start and practice guide Jan 19, 2024 am 08:57 AM

The Numpy library is one of the most commonly used data processing libraries in Python. It is widely loved by data analysts for its efficient and convenient operation methods. In the Numpy library, there are many commonly used functions that can help us complete data processing tasks quickly and efficiently. This article will introduce some commonly used Numpy functions, and provide code examples and practical application scenarios so that readers can get started with the Numpy library faster. 1. Create an array numpy.array function prototype: numpy.array(obj

An in-depth analysis of the Go language standard library: revealing the secrets of commonly used functions and data structures An in-depth analysis of the Go language standard library: revealing the secrets of commonly used functions and data structures Jan 30, 2024 am 09:46 AM

Explore the Go language standard library: Detailed explanation of common functions and data structures Introduction: Since its birth, the Go language has attracted the attention of many developers with its simplicity, efficiency, and concurrency. As a modern programming language, the Go language provides a wealth of functions and data structures in its standard library to help developers quickly build high-performance, reliable applications. This article will explore in detail some commonly used functions and data structures in the Go language standard library, and deepen understanding through specific code examples. 1. strings package: string processing function G

Common functions for PHP file operations Common functions for PHP file operations Jun 16, 2023 pm 01:15 PM

PHP is a widely used open source programming language that is widely used in the field of web development. In web development, file operation is an essential part, so it is very important to be proficient in PHP's file operation functions. In this article, we will introduce some functions commonly used in PHP file operations. fopen() The fopen() function is used to open a file or URL and returns the file pointer. It has two parameters: file name and opening method. The opening mode can be "r" (read-only mode), "w" (write mode), "a"

What are the commonly used functions in the pandas library? What are the commonly used functions in the pandas library? Nov 22, 2023 pm 01:36 PM

Commonly used functions in the pandas library include: 1. read_csv() and read_excel() functions; 2. head() and tail() functions; 3. info() function; 4. describe() function, etc. Detailed introduction: 1. read_csv() and read_excel() functions. These two functions are used to read data from CSV and Excel files. They can read the data into data frame objects to facilitate further data analysis; 2. head () and tail() functions, etc.

What are the common image processing operations in PHP programming? What are the common image processing operations in PHP programming? Jun 12, 2023 am 09:07 AM

Image processing is a very important topic in PHP programming. With the development of web applications, more and more websites need to use images to attract users' attention. Therefore, it is very important for PHP developers to master some common image processing operations. This article will introduce some common image processing operations for reference by PHP developers. 1. Image scaling Image scaling is one of the most common operations in image processing. PHP provides two methods to resize images: ImageCopyResample()

What are the common functions supported by Go language? What are the common functions supported by Go language? Mar 22, 2024 am 11:00 AM

As an open source statically typed programming language, Go language has a rich standard library and powerful functions. In the Go language, there are many commonly used functions and methods that can help us simplify the code and improve programming efficiency. The following will introduce several commonly used functions in the Go language and give specific code examples. 1. The Printf function in the fmt package. The fmt package is a standard package for formatting input and output in the Go language. The Printf function in it can output the content to the standard output stream according to the format string. Below is a

See all articles