請(qǐng)問我用DOM JS獲取不到我js動(dòng)態(tài)添加的元素的內(nèi)容
<<!DOCTYPE html>
<html>
?? ?<head>
?? ??? ?<meta charset="UTF-8"/>
?? ??? ?<title>test</title>
?? ??? ?<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
?? ??? ?<script src="test.js" type="text/javascript"></script>
?? ?</head>
?? ?<body>
?? ??? ?<button>增加</button>
?? ??? ?<ul id="ullist"></ul>
?? ?</body>
</html>
$(document).ready(function(){
?? ?$("button").click(function(){
?? ??? ?var frag=document.createDocumentFragment();
?? ??? ?var li=document.createElement("li");
?? ??? ?li.innerHTML="<input id='testli' type='button' value='切換' title='1'/>";
?? ??? ?frag.appendChild(li);
?? ??? ?document.getElementById("ullist").appendChild(frag);
?? ?})
?? ?var getli=document.getElementsByTagName("input");
?? ?getli[0].onclick=function(){
?? ??? ?alert("1");
?? ?}
})
2015-09-14
<!DOCTYPE html>
<html>
? ? <head>
? ? ? ? <meta charset="UTF-8"/>
? ? ? ? <title>test</title>
? ? ? ? <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
? ? ? ? <script src="test.js" type="text/javascript"></script>
? ? </head>
? ? <body>
? ? ? ? <button>增加</button>
? ? ? ? <ul id="ullist"></ul>
? ? </body>
</html>
<script>
? ? $(document).ready(function(){
? ? ? ? $("button").click(function(){
? ? ? ? ? ? var frag=document.createDocumentFragment();
? ? ? ? ? ? var li=document.createElement("li");
? ? ? ? ? ? li.innerHTML="<input id='testli' type='button' value='切換' title='1'/>";
? ? ? ? ? ? frag.appendChild(li);
? ? ? ? ? ? document.getElementById("ullist").appendChild(frag);
? ? ? ? ? ??
? ? ? ? ? ? var getli=document.getElementsByTagName("input");
? ? ? ? ? ? getli[0].onclick=function(){
? ? ? ? ? ? ? ? alert("1");
? ? ? ? ? ? }
? ? ? ? })
? ? })
</script>