Home >Web Front-end >JS Tutorial >Detailed explanation of the difference between the href and onclick events of a tag_Basic knowledge

Detailed explanation of the difference between the href and onclick events of a tag_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:31:312217browse

The difference between javascript in onclick is usually not noticed until I use it, but I feel a little depressed when I make an error. Read the text and the explanation is as follows:

I used to be very casual, but later I saw that the link button in .net seems to be in the form of . Today I encountered some problems using this method. , excerpts from netizens’ articles and my conclusions are placed below:

1. The onclick event of the link is executed first, followed by the action under the href attribute (page jump, or javascript pseudo link);
2. Assume that both href and onclick exist in the link. If you want the action under the href attribute not to be executed, onclick must get a false return value. If you don’t believe it, you can comment out the return false in the goGoogle function;
3. If the page is too long and has a scroll bar, and you want to perform operations through the onclick event of the link. Its href attribute should be set to javascript:void(0); instead of #, which can prevent unnecessary page jumps;
4. If a function with a return value is called in the href attribute of the link, the content of the current page will be replaced by the return value of this function;
5. There will be a difference when holding down the Shift key.
6. The problem I encountered today is that the parentNode cannot be accessed in the form of href in IE6.0.
7. Try not to use javascript: protocol as the href attribute of A. This will not only cause the window.onbeforeunload event to be triggered unnecessarily, but also cause the gif animated image to stop playing in IE.
That's all, spent a lot of time on this.

[Reason]
When using the CheckBoxList control, I want to add a link after each checkbox. In addition to clicking on the link to achieve some functions, I also need to select the checkbox.


[Link in label]

Finally implemented using parentNode.

ClickToDefine

Statement:
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