jQuery代码显示PHP提供的图像,而无需重新加载

时间:2019-06-10 12:25:10

标签: jquery

我的php页面显示一个图像滑块,当用户单击图像(.item)时,下面的div(#team_single)更改了内容,而无需重新加载页面。当用户单击滑块的箭头(.slick-prev,.slick-next,.slick-arrow)时,div的内容也会更改。

我正在寻找一种优化以下代码的方法。

我为代码重复做了一个team_push函数,但是我仍然知道它比这更好!

function team_push($image, $position, $sousposition, $title, $content) {
    $('#team_single .single-img').html($image);
    $('#team_single .position').html($position);
    $('#team_single .sousposition').html($sousposition);
    $('#team_single .title').html($title);
    $('#team_single .description').html($content);
}
var image = $('.slick-current').find("img").clone();
var position = $('.slick-current .position').html();
var sousposition = $('.slick-current .sousposition').html();
var title = $('.slick-current .title').html();
var content = $('.slick-current .content').html();
team_push(image, position, sousposition, title, content);

$('#team_slider .item').on('click', function(){
    var image = $(this).find("img").clone();
    var position = $(this).find('.position').html();
    var sousposition = $(this).find('.sousposition').html();
    var title = $(this).find('.title').html();
    var content = $(this).find('.content').html();
    team_push(image, position, sousposition, title, content);
});
$('#team_slider .slick-arrow').on('click', function(){
    var image = $('.slick-current').find("img").clone();
    var position = $('.slick-current .position').html();
    var sousposition = $('.slick-current .sousposition').html();
    var title = $('.slick-current .title').html();
    var content = $('.slick-current .content').html();
    team_push(image, position, sousposition, title, content);
});
$('#team_single .slick-prev').on("click", function(e){
    e.preventDefault();
    $('#team_slider').slick('slickSetOption', 'slidesToScroll', 1, true);
    $('#team_slider').slick('slickPrev');
    var image = $('.slick-current').find("img").clone();
    var position = $('.slick-current .position').html();
    var sousposition = $('.slick-current .sousposition').html();
    var title = $('.slick-current .title').html();
    var content = $('.slick-current .content').html();
    team_push(image, position, sousposition, title, content);
});
$('#team_single .slick-next').on("click", function(e){
    e.preventDefault();
    $('#team_slider').slick('slickSetOption', 'slidesToScroll', 1, true);
    $('#team_slider').slick('slickNext');
    var image = $('.slick-current').find("img").clone();
    var position = $('.slick-current .position').html();
    var sousposition = $('.slick-current .sousposition').html();
    var title = $('.slick-current .title').html();
    var content = $('.slick-current .content').html();
    team_push(image, position, sousposition, title, content);
});

我需要更少的代码行:)

0 个答案:

没有答案