This article introduces the use of jquery removeattr and attr event to give a tag to add and delete the OnClick event of the specific operation method, there is a need to understand the friend can refer to.
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:
Increase 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)
Set the OnClick property as: $ ("a"). attr ("onclick", "Test ();");
Delete Event
$ (selector). Removeattr (attribute)
For example, delete the onclick attribute: $ ("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:
Versions prior to jquery 1.6 do not support the attr () method's operation on the label 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.
<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 after 3 seconds </a>
</p>
The key word for deleting attributes in jquery is: removeattr Note A is uppercase
jquery also has a way to dismiss event delegation: Unbind ([EventType]). The parameter is optional and, if not selected, removes all bound events from the node.
Here you only need to dismiss the Click event, which can be handled with $ ("#s1"). Unbind ("click").