javascript
  • jquery
  • html
  • css
  • 2014-09-13 54 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

    相關問題