我正在一个项目中使用 javascript,但遇到了一个小问题。问题是我创建了一个页面,我在其中使用 JSON 对象动态创建列表。现在我需要对该列表中的元素应用双击侦听器。这是到目前为止的代码:
<HTML>
<body onload="createTree()">
<div id="tree" style="background-color:#FFD700;height:500px;width:300px;float:left;">
<b>Tree</b><br>
<script>
var JSONObject= {"className":"com.alta.entity.Person","key":"this","modifier":1,"value":"Person [id\u003d1001, firstName\u003dShiju]","level":0,"elementArray":[
{"className":"java.lang.Long","key":"id","modifier":17,"value":"1001","level":1,"elementArray":[]},{"className":"java.lang.String","key":"firstName","modifier":17,"value":"Shiju","level":2,"elementArray":[{"className":"java.lang.Character","key":"0","modifier":17,"value":"S","level":3,"elementArray":[]},{"className":"java.lang.Character","key":"1","modifier":17,"value":"h","level":4,"elementArray":[]},{"className":"java.lang.Character","key":"2","modifier":17,"value":"i","level":5,"elementArray":[]},{"className":"java.lang.Character","key":"3","modifier":17,"value":"j","level":6,"elementArray":[]},{"className":"java.lang.Character","key":"4","modifier":17,"value":"u","level":7,"elementArray":[]}]},{"className":"java.util.ArrayList","key":"addressArray","modifier":1,"value":"[com.alta.entity.Address@10045eb]","level":3,"elementArray":[{"className":"com.alta.entity.Address","key":"0","modifier":1,"value":"com.alta.entity.Address@10045eb","level":4,"elementArray":[{"className":"java.lang.Long","key":"id","modifier":17,"value":"1001","level":5,"elementArray":[]},{"className":"java.lang.String","key":"addressLine","modifier":17,"value":"This is addredss Line","level":6,"elementArray":[{"className":"java.lang.Character","key":"0","modifier":17,"value":"T","level":7,"elementArray":[]},{"className":"java.lang.Character","key":"1","modifier":17,"value":"h","level":8,"elementArray":[]},{"className":"java.lang.Character","key":"2","modifier":17,"value":"i","level":9,"elementArray":[]},{"className":"java.lang.Character","key":"3","modifier":17,"value":"s","level":10,"elementArray":[]}]}]}]}]};
document.getElementById('tree').appendChild(createUls(JSONObject));
function createUls(element){
console.log('looooop');
var ul = document.createElement('ul');
var li = document.createElement('li');
li.innerHTML = element.key;
ul.appendChild(li);
var elementArray = element.elementArray;
for(var i=0; i<elementArray.length; i++){
li.appendChild(createUls(elementArray[i]));
}
return ul;
}
</script>
</body>
我打算在所有正在创建的列表元素上放置一个双击监听器。如果有人可以请帮助我将不胜感激。如果您有任何疑问,您可以复制 Html 并查看它的作用。