Home > Article > Web Front-end > Learn more about some common methods in jQuery
As Web front-end developers, we often use the jQuery library, which is one of the most widely used JavaScript libraries in the industry. It can bring us benefits such as simplifying coding and improving development efficiency. Among them, the methods provided in jQuery can help developers quickly process DOM elements in the page, perform animations, perform event binding and other operations. In this article, we will take an in-depth look at some common methods in jQuery and their usage.
Just like we introduce other libraries, before using jQuery, we need to introduce it into our project. The most common method is to download jQuery from the official website (http://jquery.com/) and then introduce it into the project:
<script src="jquery.js"></script>
If you are using a CDN service, you can introduce it directly:
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
After introduction, we can use the methods provided by jQuery.
The selector is a very powerful function of jQuery, which can quickly locate elements in the document. The following are some basic selectors:
$('p') // 选取所有<p>元素
$('.my-class') // 选取所有class="my-class"的元素
$('#my-id') // 选取所有id="my-id"的元素
$('input[type="checkbox"]') // 选取所有type="checkbox"的<input>元素
$('div p') // 选取所有<div>元素中的所有<p>元素
In addition, there are many more complex selectors, you can Refer to jQuery's official documentation for more details.
In addition to selectors, jQuery also provides some convenient DOM operation methods. The following are some basic DOM operation methods:
$('a').attr('href') // 获取所有<a>元素的href属性值
$('a').attr('href', 'http://www.example.com/') // 设置所有<a>元素的href为http://www.example.com/
$('p').text() // 获取第一个<p>元素的文本 content
$('p').text('Hello World!') // 设置第一个<p>元素的文本为Hello World!
$('div').html() // 获取第一个<div>元素的HTML内容
$('div').html('<p>Hello World!</p>') // 设置第一个<div>元素的HTML内容为<p>Hello World!</p>
$('ul').append('<li>New Item 1</li><li>New Item 2</li>') // 在第一个<ul>元素末尾添加两个新的<li>元素
$('p').remove() // 移除所有<p>元素
jQuery provides developers with more convenient event methods than native JavaScript, so we don’t have to worry about browser compatibility and other issues. The following are some basic event binding methods:
$('button').click(function() { console.log('Button clicked!'); });
$('div').dblclick(function() { console.log('Div double clicked!'); });
$('div').mouseenter(function() { console.log('Mouse entered div!'); });
$('div').mouseleave(function() { console.log('Mouse left div!'); });
$('input').keyup(function() { console.log('Input value changed!'); });
$('form').submit(function() { console.log('Form submitted!'); });
Of course, in addition to the events listed above, jQuery also provides many other event methods that developers can use for different needs.
The last step is the operation of animation effect. Animation effect is an indispensable part of the web development process. It can make the page more interactive and visually attractive.
$('div').hide(); // 隐藏第一个<div>元素 $('div').show(); // 显示第一个<div>元素
$('div').slideUp(); // 收起第一个<div>元素 $('div').slideDown(); // 打开第一个<div>元素
$('div').fadeOut(); // 隐藏第一个<div>元素 $('div').fadeIn(); // 显示第一个<div>元素
$('div').animate({ // 隐藏第一个<div>元素 marginLeft: '50px', opacity: 0 }, 1000);
This article introduces some common methods in jQuery, covering basic selectors, DOM operations, event binding and animation effects. Although jQuery provides many methods, we can choose the appropriate method according to our needs during use to better meet development needs.
The above is the detailed content of Learn more about some common methods in jQuery. For more information, please follow other related articles on the PHP Chinese website!