Example of delegate () method usage in jQuery, jquerydelegate
This document describes the delegate () method in jQuery. Share it with you for your reference. The specific analysis is as follows:
This method adds one or more events to the child element that matches the element and specifies the function that runs when these events occur.
Syntax structure:
Copy codeThe Code is as follows: $ (selector). delegate (childofselector, type, data, function)
Parameter List:
Parameters |
Description |
Childofselector |
Defines one or more child elements to attach an event handler. |
Type |
Optional. Defines one or more event types appended to an element. Multiple event values are separated by spaces. |
Data |
Define additional data passed to the event processing function. |
Function |
Defines the function that runs when an event occurs. |
Instance code:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> helping customers </title>
<Style type = "text/css">
Li {
List-style-type: none;
Width: 150px;
Height: 150px;
Border: 1px solid green;
}
</Style>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Div"). delegate ("button", "click", function (){
$ ("Li"). slideToggle ();
})
})
</Script>
</Head>
<Body>
<Div>
<Ul>
<Li> welcome to the customer's home </li>
</Ul>
<Button> click to view results </button>
</Div>
</Body>
</Html>
The above code registers the click event handler function for the button element of the div sub-element. When you click the button, you can switch between hide and display.
I hope this article will help you with jQuery programming.