Multiple Solutions to trigger onclick events without redirecting from tag

Source: Internet
Author: User

During web page development, we often encounter the following situations:

1. A tag is only used to trigger the onclick action;
2. Show the pointer with the mouse or other visual effects similar to the label.
For example, to avoid misoperation during the delete operation, a dialog box is displayed asking the user to determine whether to delete the operation. Therefore, we often use the link <a> </a> instead of <button> to trigger the onclick event.
The Code is as follows:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function del (){
If (confirm ("are you sure you want to delete this record? ")){
Parent. window. location = "delete. jsp ";
Return true;
}
Return false;
}
</Script>
<A href = "" target = "mainFrame" class = "STYLE4" onclick = "del ()"> Delete </a>

The consequence of this is that the js Code will jump to the "execute Delete. jsp" page, and the <a> tag will jump to open an empty page. Because html itself processes the href attribute of the <a> tag, we will first execute our own defined method and then run its own method (jump Method ).

There are four solutions:
1. Do not use the tag, set css or use js for presentation (a bit complicated );
2. Use the tag, The onclick attribute, or The onclick event to return false)
For example, <a href = "" target = "mainFrame" class = "STYLE4" onclick = "del (); return false"> Delete </a>
This is an issue about the execution sequence. <a> the execution sequence of this label should be the onclick script first, and the page Jump specified by the href parameter is finally executed. If false is returned in onclick, the workflow of the <a> label can be aborted, that is, the page is not redirected to the page specified by the href parameter.
3. Use the pseudo-Protocol href = "javascript: void (0)". (This pseudo-protocol is not well written)
That is, <a href = "javascript: void (0)" target = "mainFrame" class = "STYLE4" onclick = "del ()"> Delete </a>
4. <a href = "#" class = "STYLE4" onclick = "del ()"> Delete </a>. (Always jump to the top of the current page. When there is a large amount of content on the page, there will still be a jump feeling)

Related Article

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.