2013-07-12 40 views
0

我的PHP吐出我在循環中的所有汽車使每輛車得到相同的div id和類,但不同的信息 我的問題是我的唯一插件適用於同一div這是第一個 那是因爲所有的人都被命名爲同一的slideToggle不工作的多個div

我怎樣才能使這個功能的智能知道哪個格的內容應該是隱藏或顯示

看看我的例子http://jsfiddle.net/S2HEw/

(function ($) { 
    $.fn.showHide = function (options) { 

    //default vars for the plugin 
     var defaults = { 
      speed: 1000, 
      easing: '', 
      changeText: 0, 
      showText: 'Show', 
      hideText: 'Hide' 

     }; 
     var options = $.extend(defaults, options); 

     $(this).click(function() { 
// optionally add the class .toggleDiv to each div you want to automatically close 
         $('.toggleDiv:hidden').slideUp(options.speed, options.easing); 
      // this var stores which button you've clicked 
      var toggleClick = $(this); 
      // this reads the rel attribute of the button to determine which div id to toggle 
      var toggleDiv = $(this).attr('rel'); 
      // here we toggle show/hide the correct div at the right speed and using which easing effect 
      $(toggleDiv).slideToggle(options.speed, options.easing, function() { 
      // this only fires once the animation is completed 
      if(options.changeText==1){ 
      $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText); 
      } 
       }); 

      return false; 

     }); 

    }; 
})(jQuery); 



$(document).ready(function(){ 

    $('.show_hide').showHide({ 
     speed: 250, // speed you want the toggle to happen 
     easing: '', // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI 
     changeText: 1, // if you dont want the button text to change, set this to 0 
     showText: 'View',// the button text to show when a div is closed 
     hideText: 'Close' // the button text to show when a div is open 

    }); 

}); 

的HTML,有很多這樣的人的,他們都具有相同的名稱和這就是爲什麼我的切換按鈕出現故障,

<a class="show_hide" href="#" rel="#slidingDiv">View</a> 
      <div id="slidingDiv" class="toggleDiv" style="display: none;"> 
      <div class="right"> 
</div> 
</div> 

注意,我不能更新我的PHP循環,所以我真的需要找到一種方法爲jquery知道哪個div它正在工作,即使他們都有相同的div ID?我可以讓PHP做越來越多的數字,並將其追加到類,然後在jQuery的一面?瞧,這就是IM卡

我不知道如何使這項工作動態

回答

1

你必須爲slidingDiv元素重複的ID。一個元件的id必須是唯一的

從文檔在文檔中爲id-attribure

id attribute指定其元素的唯一標識符(ID)。該值必須在元素的home subtree中的所有ID中唯一,並且必須至少包含一個字符。該值不得包含任何space characters

<!-- DIV 1--> 
<a class="show_hide" href="#" rel="#slidingDiv1">View</a> 
<div id="slidingDiv1" class="toggleDiv" style="display: none;"> 
    <div class="right"> 
     DIV 1 

    </div> 
</div> 
<!-- DIV 2--> 
<a class="show_hide" href="#" rel="#slidingDiv2">View</a> 
<div id="slidingDiv2" class="toggleDiv" style="display: none;"> 
    <div class="right"> 
     DIV 2 

    </div> 
</div> 
<!-- DIV 3--> 
<a class="show_hide" href="#" rel="#slidingDiv3">View</a> 
<div id="slidingDiv3" class="toggleDiv" style="display: none;"> 
    <div class="right"> 
     DIV 3 

    </div> 
</div> 

此外,需要使用:可見代替:隱藏

$('.toggleDiv:visible').slideUp(options.speed, options.easing); 

演示:Fiddle

+0

等待,我也會告訴PHP讓每個DIV編號不同,對不起,我說,我現在正在冒犯現在我意識到幫助抱歉的人BRB –

+0

它的工作完美的意義:)如果它很酷,我的div成功擴展,但它推動整個行在底部向下,我怎麼能做到這一點div顯示其他divs,所以它不推動底部行? –