[英]How to change a class name of dynamic contents using jquery
In my php file I have this code section to get all jpg files form a folder and put them in to img tags. 在我的php文件中,我有此代码部分,可将所有jpg文件从一个文件夹中获取并将其放入img标签中。
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'jpg') {
echo '<div class="image_slider"><img src ="' . $dir . $file . '"/></div>';
}
}
closedir($handle);
}
and then I want to change displaying image by clicking on a links call previous and next. 然后我想通过单击上一个和下一个链接来更改显示图像。
To do so I'm using jQuery this way.. 为此,我以这种方式使用jQuery。
To show first image without clicking any thing 不显示任何东西即可显示第一个图像
$( ".image_slider img" ).first().addClass( "active" );
and then for the other images to load user must clik prvs or next links. 然后要加载其他图像,用户必须单击prvs或下一个链接。
function next_img(){
$( ".image_slider img" ).closest().next( "active" );
}
but this function is not working. 但是此功能不起作用。 It doesn't change the class "active" from first one to second one.
它不会将“活动”类从第一个更改为第二个。
I feel I'm missing something. 我觉得我缺少什么。 but couldn't find it.
但找不到。
Thank you 谢谢
You need to find the current active image: 您需要找到当前的活动图像:
var $current = $('.image_slider .active');
(The $ in $current
doesnt do anything, it just shows other developers that $current
is or should be a jQuery object) (
$current
中的$current
不会做任何事情,它只是向其他开发人员显示$current
是或应该是jQuery对象)
Remove its active class 删除其活动班级
$current.removeClass('active');
Then take the current images parent and go up ( prev
) or down ( next
) the DOM to fetch the next .image-slider element. 然后将当前图像作为父图像,并向上(
prev
)或向下( next
)DOM以获取下一个.image-slider元素。
$next = $current.parent().next();
// OR
$next = $current.parent().prev();
And if $next exists (the active one could have been the last image), give it the active class: 如果$ next存在(活动的可能是最后一张图像),则为它提供活动的类:
if ($next.length) {
$next.find('> img').addClass('active');
}
These are the basics, feel free to improve the logic to gain your users experience 这些都是基础知识,请随时改进逻辑以获取用户体验
尝试这个
$(".active").removeClass("active").next("img").addClass("active");
i want to enhance your code to follow 我想增强您的代码遵循
$s='';
$first=false;
$handle=@opendir($dir);
if ($handle) {
$s='<div class="image_slider">';
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'jpg') {
$cls='';
if (!$first) {$cls='active'; $first=true;}
$s.='<img class="'.$cls.'" src ="' . $dir . $file . '"/>';
}
}
$s.='</div>';
closedir($handle);
}
else {
echo 'Error in reading image folder-or the dir doesnt exist!';
}
and about slider: you can use the the active to get the current active and then use .next()
to get the next element. 和关于滑块:您可以使用active来获取当前active,然后使用
.next()
来获取下一个元素。
i wrote a jsfiddle for you, hope you find it useful a sample jsfiddle about using .next() 我为您编写了一个jsfiddle,希望对使用.next()的示例jsfiddle有用。
the function i wrote is : 我写的功能是:
function activeNext(){
var act=$('div.active');
var img=$('.img_slider');
if (act.is(img.last())) return false;
act.removeClass('active').next().addClass('active');
}
function activePrev(){
var act=$('div.active');
var img=$('.img_slider');
if (act.is(img.first())) return false;
act.removeClass('active').prev().addClass('active');
}
just a brief explanation: 只是一个简短的解释:
1- get the current active and save it in act
1-获得当前活动并将其保存在
act
2- get all the image and store in img
2-获取所有图像并将其存储在
img
3- if current active one is first or last one then do nothing 3-如果当前活动的是第一个或最后一个,则什么也不做
4- remove current active one. 4-删除当前活动的一个。 find prev/next item and make it active
找到上一个/下一个项目并将其激活
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.