jquery delegate() method


  Translation results:

delegate

UK[ˈdelɪgət] US[ˈdɛlɪˌɡet]

n. Representative, delegation member

vt. Appointed representative; authorized to ;[Legal]Debt transfer

jquery delegate() methodsyntax

Function:delegate() method adds one or more event handlers to the specified element (a child element of the selected element) and specifies the function to run when these events occur. Event handlers using the delegate() method apply to the current or future elements (such as new elements created by a script).

Syntax: $(selector).delegate(childSelector,event,data,function)

Parameters:

ParametersDescription
childSelector Required. Specifies one or more child elements to which event handlers are attached.
event Required. Specifies one or more events to attach to the element. Multiple event values ​​separated by spaces. Must be a valid event.
data Optional. Specifies additional data to be passed to the function.
function Required. Specifies a function to run when an event occurs.

jquery delegate() methodexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("div").delegate("button","click",function(){
    $("p").slideToggle();
  });
});
</script>
</head>
<body>
<div style="background-color:red">
<p>这是一个段落。</p>
<button>请点击这里</button>
</div>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance

Popular Recommendations

Home

Videos

Q&A