jquery dynamic Remove/Add onclick attribute detailed

Source: Internet
Author: User

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

In jquery, actions for label properties are implemented using the attr () method, such as: $ ("a"). attr ("onclick") gets the OnClick property of a label, corresponding to:

Increase Event

$ (selector). attr (property name) its role is to get the value of the specified property for the specified element (the $ (selector) portion)

Such as

Set the OnClick property: $ ("a"). attr ("onclick", "Test ();");

Delete Event

$ (selector). Removeattr (attribute)

Cases

Delete onclick attribute: $ ("a"). Removeattr ("onclick");


Theoretically the above code is fine, 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 for the label OnClick Property!

? Q words to use the attr () method to manipulate the onclick attribute in the label must use JQ library 1.6 or the newer version of the library!

, 3 seconds later
  code is as follows copy code
<script Type= "Text/javascript" src= "jquery.min.js" ></script><script type= "Text/javascript"
$ (function () {
 $ (". B"). Click (function () {
  $ (this). Removeattr ("onclick");
  settimeout (function () {
   $ (". B"). attr ("onclick", "Test ();");
   },3000)
  
 })
Function Test () {}
</script>
<p
 <a class= "B" href= "# onclick=" Test (); " > Click to remove the OnClick property of the link, and then automatically add the OnClick property </a>
</p>

The key word for deleting attributes in

 
jquery is: removeattr Note that a is uppercase

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.