Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击时检索jQuery数组元素的值_Javascript_Php_Jquery_Html_Css - Fatal编程技术网

Javascript 单击时检索jQuery数组元素的值

Javascript 单击时检索jQuery数组元素的值,javascript,php,jquery,html,css,Javascript,Php,Jquery,Html,Css,我试图使用var next根据单击的div的索引获取存储在数组图像中的id。我需要这样做,因为div id可以是基于php的任何东西 JavaScript HTML 我想知道.get是否是要使用的正确jQuery处理程序。正如人们在评论中提到的,$.get是使用get-HTTP方法的AJAX请求的包装器,也许您不需要数组来实现所需的功能 但是,假设您只想获取存储在图像数组中的值,您可以使用: var next=图像[当前] 或 您可以使用.eq过滤器在给定索引处获取所需元素,而无需将所有内容存储

我试图使用var next根据单击的div的索引获取存储在数组图像中的id。我需要这样做,因为div id可以是基于php的任何东西

JavaScript

HTML


我想知道.get是否是要使用的正确jQuery处理程序。

正如人们在评论中提到的,$.get是使用get-HTTP方法的AJAX请求的包装器,也许您不需要数组来实现所需的功能

但是,假设您只想获取存储在图像数组中的值,您可以使用:

var next=图像[当前]

您可以使用.eq过滤器在给定索引处获取所需元素,而无需将所有内容存储在数组中,请在代码中尝试以下操作:

$'.gallerypreview'。单击函数{ //您可以只使用$this.attr'id;,来获取单击的div的id //下面的代码是为了示例 var current=$this.index; var galleryPreviewId=$'.gallerypreview'.eq current.attr'id'; 警告div的id为:+galleryPreviewId; };

$'.gallerypreview'.eq索引将匹配的元素集—所有具有gallerypreview类的元素—减少为指定索引处的元素集

注意:获取元素id后,可能需要使用前缀来显示id等于next:$+next.show的DOM元素


有关更多参考,请参阅

$.get是一个使用get HTTP方法的AJAX请求包装器。您根本不需要数组来完成此任务,如果您制作了一个工作小提琴,您将更快地获得帮助。
var currentImage = 1;
var numImages = 0;
var images = new Array();

$(document).ready(function() {

    $('.gallerypreview').each(function(i) {
        images.push($(this).attr("id"));
    });

    $('.gallerypreview').click(function() {
        var current = $(this).index();
        alert(current);
    });

    $('.gallerypreview').each(function() {
        numImages++;
    });

    $('.galleryrightbtn').click(function() {
        moveLeft();
        current++;
        var next = $(current).get(this);
        $('.gallerylightbox').hide(300);
        $(next).show(300);
    });

    $('.galleryleftbtn').click(function() {
        moveRight();
        current--;
        var next = $(current).get(this);
        $('.gallerylightbox').hide(300);
        $(next).show(300);
    });

});

function moveLeft() {

    if ((currentImage + 1) == numImages) {
        $('.galleryrightbtn').css('display', 'none');
        $('.galleryleftbtn').css('display', 'block');
    } else {
        $('.galleryrightbtn').css('display', 'block');
        $('.galleryleftbtn').css('display', 'block');
    }
    if (currentImage < numImages) {

        currentImage++;
    }
}

function moveRight() {

    if ((currentImage - 1) == 1) {
        $('.galleryleftbtn').css('display', 'none');
        $('.galleryrightbtn').css('display', 'block');
    } else {
        $('.galleryleftbtn').css('display', 'block');
        $('.galleryrightbtn').css('display', 'block');
    }

    if (currentImage <= numImages) {
        currentImage--;
    }
}
<div class="gallerypreview" id="<?php echo $idvalue; ?>"></div>
<div class="gallerypreview" id="<?php echo $idvalueagain; ?>"></div>
<div class="gallerypreview" id="<?php echo $idvaluewhoknows; ?>"></div>