$(document).ready(function (){
data1();
})
function data1(){
$.ajax({
type:"GET",
url:"php.php",
  beforeSend:null,
success:function Response(){
$(".category_menu").load("php.php"); 

})
}
首先在html 页面的读取了php的运行结果 php.php如下
<?php$con = mysql_connect("localhost","root","123456");  
if (!$con) {  
die('Could not connect: ' . mysql_error());  

mysql_select_db("test", $con); 
$result = mysql_query("SELECT * FROM category");
while($row = mysql_fetch_array($result)) { ?>
<div class="menu1">  
<a id="<?php echo $row['id'] ?>" href="#">
<?php echo $row['title'];?>
</a>  
</div><?php } ?>
完了我在用js 进行class操作
$(function(){
  $(".menu1").click(function(){
  $(".type").show();
$(".problem").hide();
})
})
但是js读不到 php循环输出的.menu1,点击毫无反应
这是为什么呀 求高手赐教 急!谢谢!

解决方案 »

  1.   

    生成的HTML成功了吗,用工具看一下.
      

  2.   

    <script>
    $(document).ready(function (){
    data1();
    })
    function data1(){
    $.ajax({
    type:"GET",
    url:"php.php",
    beforeSend:null,
    success:function Response(){
    $(".category_menu").load("php.php");  
    $(".menu1").click(function(){
       $(".type").show();
    $(".problem").hide();
    }) }  
    })
    }</script>这样写试试
      

  3.   

    恩 成功的 返回如下
    <div class="menu1">  
    <a id="15" href="#">
    aaaaaaaaaaa </a>  
    </div>
    <div class="menu1">  
    <a id="16" href="#">
    aaaaaaaaaaa </a>  
    </div>
    <div class="menu1">  
    <a id="17" href="#">
    aaaaaaaaaaa </a>  
    </div>
    <div class="menu1">  
    <a id="18" href="#">
    aaaaaaaaaaa </a>  
    </div>
    <div class="menu1">  
    <a id="19" href="#">
    aaaaaaaaaaa </a>  
    </div>
    <div class="menu1">  
    <a id="20" href="#">
    aaaaaaaaaaa </a>  
    </div>
    <div class="menu1">  
    <a id="21" href="#">
    aaaaaaaaaaa </a>  
    </div>
    <div class="menu1">  
    <a id="22" href="#">
    aaaaaaaaaaa </a>  
    </div>
    <div class="menu1">  
    <a id="23" href="#">
    aaaaaaaaaaa </a>  
    </div>
      

  4.   


    $(".menu1").click(function(){
                          $(".type").show();
                        $(".problem").hide();
                    })
    要是把这个JS绑定事件写在php.php里面呢?
      

  5.   

    php怎么引用js?我写在php文件里打开网页直接卡住了
      

  6.   

    <script>
    $(document).ready(function (){
            data1();
        })
        function data1(){
                $.ajax({
                type:"GET",
                url:"php.php",
                beforeSend:null,
                success:function Response(){
                    $(".category_menu").load("php.php",function(responseText,textStatus,XMLHttpRequest){
    if(textStatus.success){
    $(".menu1").click(function(){
      $(".type").show();
    $(".problem").hide();
    })
    }
    });  
                                }  
            })
        }
    </script>
      

  7.   

    写在php文件里之后问题解决了~
    不过在php里写js会不会有点非主流