<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js树型测试</title>
<style>
#tree{
    width:200px;
height:400px;
border:1px solid red;
margin-left:400px;
margin-top:100px;
}
#root{
    margin-left:10px;
float:left;
}
#change_pic{
    float:left;
height:16px;
border:1px green solid;
}
#computer{
    float:left;
height:16px;
width:180px;
border:1px solid red;
}
.listStyle{
 min-width:100px;
 width:auto;
 height:16px;
 margin-left:10px;
 margin-top:3px;
 font-size:15px; 
 font-family:"幼圆";
 font-weight:normal;
 cursor:pointer;
 color:#000000;
 border:1px solid green;
}
.pic{
     width:12px;
 height:12px;
}
.pic_name{
     float:left;
}
</style>
<script src="js/jquery-1.3.2.js"></script>
<script>
$(document).ready(function(){
      //初始化
  $("#root").append("<div id='computer' class='style1'><img src='images/computer.png' width='12px' align='bottom' />&nbsp;设备列表</div>");
  $(".style1").prepend("<img src='images/blue_velvet_003.png' class='imgStyle1' align='bottom' style=' width:12px;height:12px;'/>&nbsp;&nbsp;");
 
  
      $(".style1").click(function(){
       $("#root").append("<div style='cursor:pointer;' class='listStyle'>"+
                           "<div class='pic_name'>"+
         "<img class='pic' src='images/blue_velvet_003.png'' />&nbsp;"+
"</div>"+
   "<div class='file_name'>"+
         "<img class='file_pic' src='images/file.png' align='bottom'/>&nbsp;<font class='word_color'>"+ "aName"+ "</font>"+
"</div>"+
 "</div>");
  //alert($("#root").html())
  });
  
  //添加奇偶事件
 // $(".pic_name").toggle(openDiv,closeDiv);
  $(". pic_name").click(function(){
     alert("hi")
  });
  function openDiv(){
     alert("aa")
      var temp_src = $(this).children(".pic").attr("src");
  alert("temp_src=="+temp_src); 
  //if()
  }
  function closeDiv(){
    alert("close")
  }
});
</script>
</head><body>
    <div id="tree">
    <!--树-->
<div id="root">
</div>
</div>
</body>
</html>
我想问我的 $(". pic_name").click(function(){
     alert("hi")
  });怎么没有反应啊而且奇偶事件也没有响应怎么回事啊
难道是我动态生成的问题?原来也是这样啊

解决方案 »

  1.   

    $(". pic_name")改为$(".pic_name")试试
      

  2.   

    楼主不可以在click事件中追加事件,这样是不可以的,最好写在函数中
      

  3.   

    遇到问题debug一下就能找到问题了
    jQuery虽然方便,但也会隐藏很多可能的错误,不建议新手赖以生存alert($('. pic_name').size()); 试试
      

  4.   


    $(".style1").click(function(){ 
    $("#root").append(" <div style='cursor:pointer;' class='listStyle'><div class='pic_name'>"+ 
    " <img class='pic' src='images/blue_velvet_003.png'' />&nbsp;"+ 
    " </div><div class='file_name'>"+ 
    " <img class='file_pic' src='images/file.png' align='bottom'/>&nbsp; <font class='word_color'>"+ "aName"+ " </font>"+ 
    " </div></div>"); 
    //alert($("#root").html()) 
    $(".pic_name").click(function(){ 
       alert("hi");
      });
    });