jquery Dynamic Removal/addition of onclick properties detailed

Source: Internet
Author: User

To achieve the effect: Click on the link to remove the onclick attribute, after 3 seconds and then automatically add the onclick attribute in the tag

In jquery, the actions for tag attributes are implemented using the attr () method, such as $ ("a"). attr ("onclick") can get the OnClick property of the A tag, corresponding to:

Add Event:

$ (selector). attr (property name) The function of this is to get the value of the specified property of the specified element (the selector part)

Such as

To set the OnClick property:

$ ("a"). attr ("onclick", "Test ();");

To delete an event:

$ (selector). Removeattr (attribute)

Cases:
Delete the OnClick property:

$ ("a"). Removeattr ("onclick");

Theoretically the above code is not a problem, but in fact the above statement does not execute or error, and later found the reason:

The previous version of JQuery 1.6 does not support the attr () method in relation to the tag onclick property!

In other words, the onclick attribute in the action label using the attr () method must use the JQ library 1.6 or the updated library version!

The code is as follows:

<Scripttype= "Text/javascript"src= "Jquery.min.js"></Script><Scripttype= "Text/javascript">$(function(){ $(". b"). Click (function(){  $( This). Removeattr ("onclick"); SetTimeout (function(){   $(". b"). attr ("onclick","test ();"); }, the)  }) })functionTest () {}</Script><P> <aclass= "B"href="#"onclick= "Test ();">Click to remove the OnClick property of the link, and then automatically add the OnClick property after 3 seconds</a></P>

The key word for deleting attributes in jquery is: removeattr Note A is uppercase

jquery Dynamic Removal/addition of onclick properties detailed

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.