Some jquery problems that beginners often encounter _ jquery

Source: Internet
Author: User
Recently, Jquery ~ was used in the project ~ New Users learn jquery .~ I encountered some & quot; strange & quot; problems (please do not make a brick). Thanks to the support of ClassYuan blog by the ClassYuan boss. http://www.classyuan.com/.the following is a few notes:
1. When loading an event with an HTML element, the event will be executed simultaneously during loading.
Error code:

The Code is as follows:

$ ("# BtnLoad"). bind ("click", GetProduct ());


Correct code

The Code is as follows:

$ ("# BtnLoad"). bind ("click", function () {GetProduct ()});


In this Code, I ignored the Bind method. The Api interpretation is bind (type, [data], fn)
I am wrong about fn as a simple function .. as a result, this code will execute this fn once during loading ..
This is an oversight of the writing format.
2. variable scope issues. (It seems that this is not a jquery issue, but the difference between js and. net ..)

The Code is as follows:

Function text (){
2 for (var I = 0; I <3; I ++ ){
3 $ ("delete "). attr ({id: "hr _" + I, href: "javascript :;"}). addClass ("btnCss "). bind ("click", function () {tes (I )}). appendTo (". p_list ");
4}
5}
6 function tes (id ){
7 alert (id );
8}


This function. I want him to generate the corresponding id for alert... the result... the answer is very good. The function of the three labels... It is 3 ..
I have read it...
Variable scope problem, the parameter for passing a tes function is the value of I after the end of the loop, so all is 3.
This seems to be different from. net.
Finally, the solution--... passed in directly $ (this ).
3. event execution sequence Problems

The Code is as follows:


2
3


This image is bound with an onclick event. The event content adds an href attribute to the parent of the tag.
However, after the event is executed, the link to the tag is displayed. Analyzed.
Click is executed before href, that is, when the image is clicked, the tag has href, And the tag is triggered at the same time.
Solution... remove the outer a tag and then modify the event

The Code is as follows:

Idwrap ('');


The above is the first time that jquery has been used by my newbie. Stay here as a course of growth ..

First question
$ ('# Btnload'). bind ('click', GetProduct );
Remember, the binding event is bound to a function, while GetProduct (), with parentheses, returns the value of this function. The returned value of your GetProduct is obviously not a function.
$ ("# BtnLoad"). bind ("click", GetProduct ());
Changed:
$ ("# BtnLoad"). bind ("click", GetProduct );
Try.
GetProduct () is to execute a function and then return the value, but the returned value is not of the fn type.

Second question
Actually. NET also has, this is the closure problem, in the closure is the address of the I variable, and I is constantly changing, so the value obtained after accessing this address is also changing, so all the tes uses the final value of the variable I, which is 3.
This problem is solved using the following method (I simplified the code ):
For (var I = 0; I <3; I ++ ){
(Function (I ){
$ ('Delete'). appendTo ('. p_list'). bind ('click', function () {tes (I );});
}) (I );
}
If you cannot understand the writing principle, it doesn't matter. Remember this form first. If you encounter similar problems in the future, write them according to this idea, that is, set a layer (function () {xxx })();
Speaking of the original image, it is actually very simple. When using a function, I is passed as a basic type variable by value, that is, the current I value will be copied to this function, therefore, each time this anonymous function is called, I is independent of each other and will not be affected by the outer.

Third question
When a does not give the href attribute, href is the current page address by default, so you will jump after clicking it. So there are several popular Processing Methods on the Internet:
1. dfa
2. dfa
3. dfa

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.