如果要选择变量'#'+id 下的 p 标签 下的 .link 类
应该怎样写呢?$function show(id){
$('#'+id > p > .link);  ---自己想的 不过是错的 呵呵
}

解决方案 »

  1.   

    $('#'+id+' > p > .link')
      

  2.   


    <!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>5_14</title>
    <script src="jquery-1.3.2.js"></script>
    <script language="javascript" type="text/javascript">
    $(document).ready(function(){

    });
    function test(){
    var id = "#test";
    var txt = $(id).find("[class=link]").length;
    alert(txt)

    </script>
    </head><body>
    <div id="test" onclick="test()">
    <p>
         <div class="link">test</div>
            <div class="link">test</div>
            <div class="link">test</div>
            <div class="link1">test</div>
        </p>
    </div>
    </body>
    </html>
    不用p那个标签
      

  3.   


    $('#'+id+'>p>.link')不要有空格
      

  4.   


    <script type="text/javascript">
    //<![CDATA[
    function foo(){
        var id = "foo";
        var txt = $('#'+id+' > p > .link').length;
    //都可以,加不加> 或者空格都可以
    //var txt = $('#'+id+'  p  .link').length;
    //var txt = $('#'+id+'>p>.link').length;
        alert(txt);

    $(function(){
    foo();
    });
    //]]>
    </script>
    <div id="foo"><p>
            <span class="link">test</span>
            <span class="link">test</span>
            <span class="link">test</span>
            <span class="link1">test</span>
    </p></div>
      

  5.   

    有一点要提示的是像#2的是错误的,p是段落,p里面不能有div(块)
    所以出现的结果是0,换成span就可以了
      

  6.   

    $("#" + id + ' p .link')