Necessity of preventDefault in javascript development

Source: Internet
Author: User

If this parameter is not added, it seems that only ie6 has encountered problems. See: changed the image address, the image disappears, the style is loaded, but the style file is gone. Just like the download of A resource is interrupted. The correct explanation is that when you use element A in IE6 as A button, remember to call preventDefault () in the onclick event (), IE6 will force the current active HTTP connection to close, leading to abnormal loading of other resources or scripts being requested. However, if the resource has been cached, this problem will not occur.
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
</Head>
<Body>
<H2> it will appear in ie6. If you click "src" to change the image, the image will not appear. preventDefault or return false is required. <A id = "J_btn" href = "javascript: void (0)"> click to change the image address </a>

<Script type = "text/javascript">
Var $ = function (id) {return typeof (id) = "string "? Document. getElementById (id): null };
Var logo_bd = 'HTTP: // www.bkjia.com/uploads/allianz 131017/013sb114-0.gif ';
Var logo_go = 'HTTP: // www.google.com.hk/intl/zh-CN/images/logo_cn.png'
$ ("J_btn"). onclick = function (event ){
Event = event | window. event;
// Event. preventDefault? Event. preventDefault (): event. returnValue = false;
$ ("J_img"). src = $ ("J_img"). src = logo_bd? Logo_go: logo_bd;
}
</Script>
</Body>
</Html>

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.