我的PHP将我的所有车循环吐出,因此每辆车都具有相同的div ID和类,但信息不同我的问题是我的插件仅适用于同一div,这是第一个,因为它们都被命名为相同的
我如何才能使此功能变得智能,以了解应该隐藏或显示哪些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端?看到这就是我卡住的地方
我不知道如何使这项工作动态
您有重复的slidingDiv
元素ID 。id
元素的元素在文档中必须是唯一的
从doc获得ID属性
的id属性指定了其元件的唯一标识符(ID)。该值在元素的主子树的所有ID中必须是唯一的,并且必须至少包含一个字符。该值不得包含任何空格字符。
<!-- 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>
另外,需要使用:visible而不是:hidden
$('.toggleDiv:visible').slideUp(options.speed, options.easing);
演示:小提琴
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句