日期:2014-05-16 浏览次数:20419 次
var str = "";
var forTree = function(obj){
for(var i in obj){
var urlstr = "";
var links="#";
//var id=guid();
if(typeof obj[i] == "object"){ //如果有子节点
//alert(i);
urlstr = "<div><span>"+ i +"</span><ul>";
}else{
urlstr = "<div><span><a href="+links+" value="+obj[i]+">"+ i +"</a></span><ul>";
}
str += urlstr;
if(obj[i] != null && typeof obj[i] == "object"){
//alert(obj[i]);
forTree(obj[i]);
}
str += "</ul></div>";
}
return str;
}
/*添加无级树*/
document.getElementById("menuTree").innerHTML = forTree(json);
/*树形菜单*/
var menuTree = function(){
//给有子对象的元素加[+-]
$("#menuTree ul").each(function(index, element) {
var ulContent = $(element).html();
var spanContent = $(element).siblings("span").html();
if(ulContent){
$(element).siblings("span").html("[+] " + spanContent)
}
});
$("#menuTree").find("div span").click(function(){
var ul = $(this).siblings("ul");
var spanStr = $(this).html();
var spanContent = spanStr.substr(3,spanStr.length);
if(ul.find("div").html() != null){
if(ul.css("display") == "none"){
ul.show(300);
$(this).html("[-] " + spanContent);
}else{
ul.hide(300);
$(this).html("[+] " + spanContent);
}
}
});
$("#menuTree").find("div span a").click(function(){
alert($('a').val());//我想要在这边弹出点击过后的节点值
alert('===============');
});
/*
$("a").each(function () {
alert($(this).val() + ":" + $(this).text());
})
*/
}()
document.getElementById("login").onclick = function(event){
event = event
------解决方案--------------------
window.event;
var target = event.srcElement
------解决方案--------------------
event.target,
context = target.innerHTML;
console.log(context);