


A compilation of some jquery problems that novices often encounter_jquery
ClassYuan's blog. http://www.classyuan.com/. Here are some examples:
1. When loading an event with an HTML element, the event will be executed at the same time as it is loading.
Error code:
Correct code
I ignored the Bind method in this code. The explanation of Api is bind (type,[data],fn)
I mistakenly regarded fn as a simple function. As a result, this code will execute the fn once when loading..
This is an oversight in writing format. I hope someone who knows the reason can give me some advice.
2 The problem of variable scope. (It seems that this is not a problem with jquery. It is the difference between js and .net..)
2 for (var i = 0; i < 3; i ) {
3 $("Delete ").attr({ id: "hr_" i, href: "javascript:;" }).addClass("btnCss").bind("click", function() { tes(i) } ).appendTo(".div_list");
4 }
5 }
6 function tes(id) {
7 alert(id);
8 }
This function. I want it to alert the corresponding id. The result is that the answer is very agreeable. It is a function with 3 tags. When it pops up, it is all 3..
Take a look. .Understand the problem of
variable scope. The parameter passed to the tes function is the value of i after the loop ends, so all are 3.
This seems to be different from .net.
Finally, the solution - -.. Just pass $(this) in
3. The problem of event execution order
Bind the onclick event to this image, and the content of the event It is to add an href attribute to the parent of the
But after the event is executed, it will jump directly to the link of the a tag. After analysis.
It should be that the click is executed before the href, that is, when the image is clicked, the A tag already has the href, and then the A tag is triggered at the same time.
Solution.. Remove
The above are the problems that I, a novice, encountered when using jquery for the first time. I will leave them here and regard them as a process of growth. Dear masters and veterans, please don’t criticize me.
The first one Question
$('#btnLoad').bind('click', GetProduct);
Remember, the binding event is tied to a function, and GetProduct(), after adding parentheses, gets this The return value of the function. And the return value of your GetProduct is obviously not a function
$("#btnLoad").bind("click",GetProduct());
is changed to:
$("#btnLoad"). bind("click",GetProduct);
Try it.
GetProduct() executes a function and then returns a value, but the returned value is not of type fn.
The second problem
In fact, it also exists in .NET. This is a closure problem. What is obtained in the closure is the address of the i variable, and i is constantly changing, so after accessing this address The obtained value is also changing all the time, so all tes use the last value of the i variable, which is 3
This problem is solved as follows (I simplify the code):
for (var i = 0; i < 3; i ) {
(function(i) {
$('Delete').appendTo('.div_list').bind('click', function() { tes(i); });
})(i);
}
If you can’t understand the principle of writing this way, it doesn’t matter. Remember this form first, and you will encounter similar problems in the future. The problem is written according to this idea, that is, a layer of (function() { xxx })();
Speaking of the original painting, it is actually very simple. When passing the function, i, as a basic type variable, is passed by value. , which means that the current value of i will be copied to this function, so every time this anonymous function is called, i is independent of each other and will not be affected by the outer for
The third question
When a does not give the href attribute, href defaults to the address of the current page, so you will jump after clicking. Therefore, there are several popular processing methods on the Internet:
1.dfa
2.dfa
3.dfa

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



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

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

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:

What are the common problems encountered in the Calabash Man app? I believe that many friends will encounter various problems with this app. I wonder if any players have encountered it? Anyway, the editor often encounters it. In order to prevent my friends from encountering various problems like the editor, I started to look for various limited exemption methods. Therefore, the editor below will bring a summary of the most common problems to all users. If you are still encountering various problems, please refer to them quickly. Summary of questions and answers on Huluxia app QWhat is root? How to get root on mobile phone? Simply put, root refers to the user with the highest administrative rights in the Android system. By using third-party root tools, many phone models can be easily

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? 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

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:
