jQuery动态添加li标签并添加属性和绑定事件方法
代码如下:
<%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="http://www.daimajiayuan.com/download/jquery/jquery-1.10.2.min.js"></script> <!-- 3.0 --> <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> </head> <body> <ul id="listproject" class="list-group"> <script type="text/javascript"> var JSarray = new Array(); JSarray[0] = "array0"; JSarray[1] = "array1"; JSarray[2] = "array2"; JSarray[3] = "array3"; JSarray[4] = "array4"; for(var i = 0;i<5;i++){ $("#listproject").append("<li id=li"+i+">"+JSarray[i]+"</li>"); //在ul标签上动态添加li标签 $("#li"+i).attr("class",'list-group-item'); //为li标签添加class属性 } $('li').on('click',function(){ //绑定事件 alert("事件绑定成功!"); }); </script> </body> </html>
运行结果:
相关推荐
liqinglin0 2020-06-01
jvm 2020-05-30
tztzyzyz 2018-09-07
madanling 2018-12-11
mfkpum 2018-11-14
suis 2019-07-25
zhuxue 2019-06-28
Danielmumu 2016-06-22
人心 2019-06-27
hackcat 2015-04-23
超级赛亚人 2014-11-15
无情 2014-11-14
asdjkl 2014-09-09
JeWangZhe 2013-07-30
无情 2013-07-02
JAVALin 2013-05-29
人心 2019-06-25