Home 类库下载 Other libraries Tips for using jquery

Tips for using jquery

Nov 07, 2016 pm 04:49 PM

The birth of jquery makes it easier for our front-end novices to operate web interactions.

Today I will bring you some tips on using jquery.

jquery blocks browser default behaviors such as hyperlinks.

$("a").click(function(event){
  event.preventDefault();
});
Copy after login

Jquery triggers the carriage return event

 $(function () {
    $('#target').bind('keyup', function (event) {
       if (event.keyCode == 13) {
          alert("Hello~");
       }
   });
});
Copy after login

Get the value of select

jquery can get the value of select just like getting the textbox value: $('select').val();

Copy text

Use window.clipboardData .setData('text', text);

You can put the text text into the system clipboard to realize the text copy function. However, this method is only supported by IE.

Google Chrome and Foxfire are not supported. Therefore, when using it, you should first determine whether the browser supports:

if (window.clipboardData) {  window.clipboardData.setData('text', text); }
Copy after login

Select text

For input or textarea text selection, jquery provides a simple function: select(). When calling it, you need to ensure that the text The box is visible and has focus.

$("#txtSample").focus().select();  //现货的焦点,然后选择文本
Copy after login

Mouse events

mouseover and mouseout, mouseenter and mouseleave; these two sets of events are triggered when the mouse moves in and out of an element,

The biggest difference between them is: mouseover and mouseout bubble, if the mouse moves to Their child elements will also trigger this event,

and mouseenter and mouseleave will not bubble. ​​

This difference is very important!

Extension of jQuery object

    $.extend(target,prop1,propN):
Copy after login

Extends an object with one or more other objects and returns the extended object. This is the inheritance method implemented by jquery. For example:

    $.extend(settings, options);
Copy after login

Merge settings and options, and return the merged result to settings, which is equivalent to options inheriting setting and saving the inherited result in setting.

    var settings = $.extend({}, defaults, options);
Copy after login

Merge defaults and options, and return the merged result to the setting without overwriting the default content. Can have multiple parameters (merge multiple items and return)

jQuery deletes items in the array

As mentioned in Tip 7, use the $.grep() method to delete elements in the array.

var array = ['a', 'b', 'c']; 
$.grap(array, function(value, index){return value=='b';}, true);
Copy after login

The above code will delete the element 'b' in the array.

jQuery array processing

$.each(obj, fn);
Copy after login

Traverse obj, obj is the array or object to be traversed; fn is the processing function, and the optional parameters are index and content, such as var fn = function(index, content){}; If you need to end the traversal, please return false, other return values ​​will be ignored.

 This method can be used to process JSON data objects.

$.inArray(obj, array);
Copy after login

  Determine whether the obj object is contained in the array array. If it exists, return the corresponding subscript. If it does not exist, return -1;

$.map(array, fn);
Copy after login

  Convert the elements in one array to another array. array is the array that needs to be converted, and fn is the processing function; the return value of this method is a new processed array.

$.merge(array1, array2);
Copy after login

  Merge two arrays; copy the contents of array2 to array1 and return the result. The merge method will not remove duplicates. You need to use $.unique() to remove duplicates.

$.unique(array);
Copy after login

  Remove duplicates from an array.

$.grep(array, fn, [invert]);
Copy after login

  Filter the elements in the array; this method calls the fn method for each object in the array array;

  invert optional parameter; if "invert" is false or not set, the function returns the array returned by the filter function true elements, when "invert" is true, the set of elements that return false in the filter function is returned.

 This method is often used to delete elements in an array

Remove the spaces at the beginning and end of the string

JS does not provide a trim function for us to remove the null characters in both sections of the string. This function has been extended in jQuery:

$ .trim(str): Remove whitespace characters at both ends of the string.

For example: $.trim(" hello, how are you? "); //Return "hello, how are you? "

Add events and remove events

It is very convenient to add events to a jQuery object :

$('#btn').click(fn);
$('#btn').bind('click', fn);
Copy after login

jQuery provides a mechanism to provide multiple handlers for events of an object. After we add a click event handler, we can continue to add it without overwriting the previous handler.

Remove the bound event subscription when the unbind method is called:

$('#btn').unbind();    //将会移除所有的事件订阅
$('#btn').unbind('click')  //将会移除click事件的订阅
Copy after login

Get an item in the jQuery object collection

For the obtained element collection, you can use eq or get to get an item (specified by index) (n) method or index number acquisition, please note that eq returns a jquery object, while get(n) and index return a dom element object. For jquery objects, you can only use jquery methods, and for dom objects, you can only use dom methods. For example, you want to get the content of the third

element. There are two methods:

$("div").eq(2).html();      //调用jquery对象的方法
$("div").get(2).innerHTML;   //调用dom的方法属性
Copy after login

Conversion of jQuery objects and Dom

Dom objects can be converted to jQuery objects through $(dom); for example:

 $(document.getElementById('#myDiv'))

jQuery objects It is an index itself, you can get the Dom object through subscript; you can also use the method get() to get the Dom object; for example:

$("div")[0];    //获取第一个Dom对象
$("div").get(0);  //同样获取第一个Dom对象
Copy after login

Intelligence sensing in an independent js file

Add at the beginning of the js file: ///


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

Video Face Swap

Video Face Swap

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

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)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

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? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

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

In-depth analysis: jQuery's advantages and disadvantages In-depth analysis: jQuery's advantages and disadvantages Feb 27, 2024 pm 05:18 PM

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

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 Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

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: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

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:

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

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

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

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

See all articles