动态绑定元素不订阅CSS规则[关闭]

I'm using ajax to dynamically populate a menu. The problem is the dynamically created elements do not follow the CSS rules, although they're created with the right classname. This is surprising.

Here's my HTML:

<div id='menu1'>
<ul class='menu'>
<?php
$sql = /// /* some sql  string, which works */
$result = mysql_query($sql);
    while($row = mysql_fetch_array( $result ))
    {
        $prod_id = $row['product_id'];      
        $prod_name = $row['product_name'];      
        echo "<li data-title='$prod_id' class='menu1_item'>".$prod_name."</li> ";
    }
?>
</ul>
</div>

<div id='menu2'>
<ul class='menu'>
</ul>
</div>

<div id='menu3'>
<ul class='menu'>
</ul>
</div>

Here's my jquery:

    $(".menu1_item").click( function() {
    $.ajax({
    type: "POST",
    data: { m: '2', id: $(this).attr('data-title') },
    url: "fetch_designs.php",
    success: function(msg){
            if (msg != ''){
            $("#menu2").html(msg).show();
            $(".menu2_item").bind('click',function() {
                        $.ajax({
                        type: "POST",
                        data: { m: '3', id: $(this).attr('data-title') },
                        url: "fetch_designs.php",
                        success: function(msg){
                        if (msg != ''){
                        $("#menu3").html(msg).show();
                            }
                            }
                        });
                    //end menu2 click
                    });
                //end if
                }
            //end success
            }
        });
    //end menu1 click
    });

My CSS is as follows:

ul.menu li
{
    cursor:pointer;
    list-style: none;
}

#menu1, #menu2, #menu3
{
    margin:50px;
    position:relative;
    display:block;
    float:left;
}

.menu1-item .menu2-item .menu3-item
{
    padding:4px;
    background-color:lightgray;
    color:black;
    cursor:pointer;
}

And the PHP code in fetch-designs.php which generates the new menu elements looks like this:

$nextmenu = $_POST['m'];
$id = $_POST['id'];
if($nextmenu == '2')
{
    // do something, and then finally generate new menu item as below:
    echo"<li data-title='$tpl_id' class='menu2_item' >".$tpl_name."</li> ";
    }
}
elseif ($nextmenu=='3')
{
        // do something, and then finally generate new link as below
        echo"<li class='menu3_item' ><a href='design_product.php?id=$d_id'>$d_name</a></li> ";
}
查看全部
dongzhuo1733
dongzhuo1733
2012/09/15 09:36
  • css
  • php
  • ajax
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复