javascript
  • jquery
  • html
  • css
  • 2014-09-13 58 views 0 likes 
    0

    我有以下代码,请注意,即时通讯这样一个noob如何可以使用jquery脚本增加div的数量?增加div id与jquery

     if($res >= 1){ 
         $i=1; 
          while($row = mysqli_fetch_array($qry)){ 
    
          echo "<div class='imgAddCntr'> 
             <div id='div".$i."' class='contentbox'>  
             //content here 
             </div> 
             <div id='imgAdd'> 
             <img class='images' alt='CDL Training' src='".$imgLInk.$row['img']."'/> 
             </div> 
            </div>"; 
            $i++; 
           } 
          } 
    

    进出口试图针对上述循环及其DIV值

    $(document).ready(function() { 
    
         $('#div1').hide(); 
    
          $(".images").hover(
           function() { 
            $("#div1").show(); 
            }, 
           function() { 
            $("#div1").hide(); 
          }); 
        }); 
    

    任何帮助将不胜感激

    +1

    请提供更多的细节,你在不清楚 – 2014-09-13 06:32:41

    +0

    ID的问题应该是唯一的 – 2014-09-13 06:57:55

    回答

    1

    使用jQuery属性选择jQuery("[attribute='value']")

    $(document).ready(function() { 
        $('[id^=div]').hide(); 
        $(".images").hover(function() { 
         $(this).closest('div.imgAddCntr').find('[id^=div]').show(); 
        }, function() { 
         $(this).closest('div.imgAddCntr').find('[id^=div]').hide(); 
        }); 
    }); 
    

    或干脆避免id并使用class代替,它无线会是更好的方式来做到这一点

    PHP

    if ($res >= 1) { 
        while ($row = mysqli_fetch_array($qry)) { 
         echo "<div class='imgAddCntr'> 
             <div class='contentbox'>  
             //content here 
             </div> 
             <div class='imgAdd'> 
             <img class='images' alt='CDL Training' src='".$imgLInk.$row['img']."'/> 
             </div> 
            </div>"; 
        } 
    } 
    

    jQuery的

    $(document).ready(function() { 
        $('.contentbox').hide(); 
        $(".images").hover(function() { 
         $(this).closest('div.imgAddCntr').find('.contentbox').show(); 
        }, function() { 
         $(this).closest('div.imgAddCntr').find('.contentbox').hide(); 
        }); 
    }); 
    
    +0

    它隐藏了他们,但它不显示他们悬停我欣赏你的帮助 – 2014-09-13 06:55:54

    +0

    也许我的PHP不增加价值 – 2014-09-13 06:57:01

    +0

    我太如此瑞在我的实际代码中有拼写错误这个作品 – 2014-09-13 07:01:33

     相关问题

    • 暂无相关问题^_^