dongzhuo1733 2012-09-15 09:36
浏览 61
已采纳

动态绑定元素不订阅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> ";
}
  • 写回答

1条回答 默认 最新

  • douying8666 2012-09-15 09:39
    关注

    Your CSS rule contains dash(-) but your js contains underscore(_) for class names.

    <li data-title='$prod_id' class='menu1_item'>
                                          ^-------- underscore
    

    but

    .menu1-item .menu2-item .menu3-item
          ^-----------^-----------^----- but these are dash
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?