An introduction to learning jQuery with example code_jquery
程序代码
window.onload = function(){ ... } .
访问HTML文档的元素,必须先载入文档对象模型(DOM)。当window.onload函数执行的时候,说明所有东西已经载入,包括图像和横幅等等。要知道较大的图片下载速度会比较慢,因此用户必须等待大图片下载完毕才能看到window.onload()执行的代码效果,这样就花费了很长的等待时间,这不是我们想要的。
对于此,jquery提供了一个"ready"事件,你可以使用以下的代码片段:
程序代码
$(document).ready(function(){//获取文档对象就绪的时候,不需要等待图片等下载完成。
// 你的代码
});
$(document)意思是说,获取整个网页文档对象(类似的于window.document),$(document).ready意思就是说,获取文档对象就绪的时候。
上面这段代码的意思是检查文档对象直到它能够允许被操作(译者注:这样做比window.onload()函数要快的多,因为只要文档对象载入完成就能够执行代码了,而不需要等待页面中的图片下载是否已经完成)---这是我们想要的。好了 ,其他的也不多说了,我们开始来用jQ写几个简单的例子。
1,demo1: --鼠标点击时的触发
首先,我们尝试鼠标点击超链接时触发某些行为。在ready函数里加入以下代码:
程序代码
$("p").click(function(){//获取所有段落p的对象,为其加上点击事件,需要加在readey中
// 你的代码
});
2,demo2:--增加 CSS Class
另外一个事情就是,一个共同的任务:增加或移除元素的css class,例如:
程序代码
$("a").addClass("test");
$("a").removeClass("test"); //样式的切换可以通过 $("p").toggleClass("selected");
3,demo3:--show( )和html()的使用
$("a").addClass("test").show().html("foo");//jquery方法可以连写
// how( ):显示隐藏的匹配元素。
//html("info"):设置每一个匹配元素的html内容。
4,demo4:--特效hide()
$("a").click(function(){
$(this).hide("slow");//对象慢慢的消失、隐藏
return false; //表示不会跳转,等同js
});
5,demo5:---收缩展开功能
$(document).ready(function(){
$("#head").click(function(){
$("#content").slideToggle("slow",function(){ alert("Hello,cssrain.."); } );
});// slideToggle(speed, callback)高度变化切换可见性,完成后可触发一个回调函数
});// speed "slow", "normal", or "fast" 也可以指定一数值
6,demo6:--appendTo的用法
{$("#head2").click(function()
{$("
").appendTo("#ccc");});}
//appendTo(): Append all matching elements to another, specified element set, that is, add sub-nodes
//append(): Add sub-nodes to an element
7, demo7:--The table changes color every other row, the mouse slides over the color, and the color changes when clicked.
Code explanation:
I have already put the explanation in the example, so I will not post comments here.
The examples used are: mouseover(), addClass(), mouseout(), removeClass(), click(),
toggleClass(), tr:even and other methods.
In addition, the difference between toggle() and toggleClass() is explained.
In addition, in this example, I used chain operation. You can view the explanation in chain operation.txt.
8, demo8:--toggle() usage:
$("p").toggle()//Switch the visible state of the element, but please note that this affects all p, You can also switch between two methods toggle( Function even, Function odd ).
9, demo9: --hover() usage:
Hover(function over, function out)//Imitate hover events
$("#orderedlist tr").hover(function over ,function out ) //Add
10,demo10:-- $ to all rows of a table. You can also use jQuery instead of
$(document).ready(function(){// Your Code});//You can also use jQuery instead of $
jQuery(document).ready(function(){
jQuery(".").click(function(){jQuery(this).toggleClass ("")})
});//The advantage is that you may use other js libraries that also use $, which may conflict. jQuery instead of $ is a safer way of writing.
Also:
$(document).ready(function(){//your code});//Abbreviation: $(function() {//your code} );
11, demo11:--each—Usage of find
$("#orderedlist").find("li").each(function(i) { })
//find("li") finds all li elements, and each() executes the method on each object in the collection
//each(Function function fn) to match each The element is used as a context to execute a function
12, demo12:--parents() usage:
$(this).parents("p").addClass("highlight");// The nearest label
this.parent()//refers to the parent node of the object
13, demo13:--Usage of load():
$("#feeds") .load("FAQ1.html",function() { alert("load is done");}
//From the remote one Load HTML from the file and inject it into the DOM
14, demo14: --next usage:
.next()//Get the next sibling node of the object
Package download addressjQuery beginners learning example code set

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.