Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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
jquery和javascript在类之间切换图像标记_Javascript_Jquery_Html_Class_Toggleclass - Fatal编程技术网

jquery和javascript在类之间切换图像标记

jquery和javascript在类之间切换图像标记,javascript,jquery,html,class,toggleclass,Javascript,Jquery,Html,Class,Toggleclass,我似乎不能把这件事做好。请帮忙。当用户单击div时,代码应该切换img标记的类 这是HTML <li><img src="IMG/Gallery/val_day.jpg" id="img27" style="width: 100%;" alt="val_day" /></li> </ul> </div> <p id="previousLink" h

我似乎不能把这件事做好。请帮忙。当用户单击div时,代码应该切换img标记的类

这是HTML

        <li><img src="IMG/Gallery/val_day.jpg" id="img27" style="width: 100%;" alt="val_day" /></li>
            </ul>


        </div>
            <p id="previousLink" href="#">«</p>
        <p id="nextLink" href="#">»</p>
        </div>

}))

我看到的问题是jQuery选择器如何查找图像

删除撇号。例如,更改
var$activeImg=“”#img“+Gallery.picNum+”至:

var $activeImg = "#img" + Gallery.picNum.toString();
第二个$activeImg也应该做同样的事情

我的另一个评论是关于公约。根据惯例,根据我的经验,表示jQuery对象的变量应该以$作为前缀$activeImg似乎不是jQuery对象。这是一根绳子。使用
activeImageSelector
(建议)

更新的JS

$(document).ready(function () {
    var Gallery = {
        li: [''],
        picNum: 0,
        nextPic: function () {
            $('#nextLink').click(function () {
                var activeImageSelector  = "#img" + Gallery.picNum.toString();
                $(activeImageSelector ).toggleClass('activePic');
                Gallery.picNum++;
                activeImageSelector  = "#img" + Gallery.picNum.toString();
                $(activeImageSelector ).toggleClass('activePic');

            });
        }
    }
});

由于没有足够的信息,我看到的第一个问题是jQuery选择器如何查找图像。删除撇号。例如,更改
var$activeImg=“”#img“+Gallery.picNum+”
to
var$activeImg=“#img”+Gallery.picNum.toString()
$(document).ready(function () {
    var Gallery = {
        li: [''],
        picNum: 0,
        nextPic: function () {
            $('#nextLink').click(function () {
                var activeImageSelector  = "#img" + Gallery.picNum.toString();
                $(activeImageSelector ).toggleClass('activePic');
                Gallery.picNum++;
                activeImageSelector  = "#img" + Gallery.picNum.toString();
                $(activeImageSelector ).toggleClass('activePic');

            });
        }
    }
});