jquery Learning record One: jquery dynamically adding HTML elements after setting their binding events

Source: Internet
Author: User
Tags pear

1. jquery dynamically adds HTML elements (for example:)

<div id= "Testdiv" >

<ul></ul>

</div>

Let's say we're going to add the <li> bind click event to the UL dynamically to produce the following results

<div id= "Devices_div" >

<ul>

<li name= "Apple" >apple</li>

<li name= "Pear" >pear</li>

</ul>

</div>

<script>

function test (name) {

Alert ("I ' M" +name);

}


2. Set the Click event using the On method

$ ("#devices_div ul"). Unbind (' click '). On ("Click", "Li", function () {
var Chooseitemstatus = $ (this). attr (' class ');
var deviceId = $ (this). attr ("DeviceId");

if (Chooseitemstatus = = "Chooselistitem") {
$ (this). attr (' class ', ' Chooselistitem choosed ');
Deviceids.push (DEVICEID);
} else {
$ (this). attr (' class ', ' Chooselistitem ');
Delete the selected element
for (var i = 0;i < Deviceids.length; i++) {
if (deviceids[i] = = deviceId) {
Deviceids.splice (i, 1);
Break
}
}
}
});


This article is from the "Sheep" blog, please make sure to keep this source http://5934497.blog.51cto.com/5924497/1676237

jquery Learning record One: jquery dynamically adding HTML elements after setting their binding events

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.