简体   繁体   English

jQuery有条件,否则语句无效?

[英]jQuery conditional, else statement invalid?

I am working on a small sample gallery for a project which moves images with a right/left button by removing and reassigning classes for identification, then appending images to a display element. 我正在为一个项目的小型样本库工作,该项目通过移除和重新分配用于标识的类,然后将图像附加到显示元素,从而通过左右按钮移动图像。

I have tried introducing an if/else conditional statement for one of my variables where the "else" statement is intended to select the image opposite the active image if it is at the end of the array (if say the active image is the last image in the gallery, I want the else statement to select for the first image for the RIGHT button). 我尝试为我的变量之一引入if / else条件语句,其中“ else”语句用于选择与活动图像相对的图像(如果它位于数组的末尾)(如果说活动图像是最后一个图像)在图库中,我希望else语句为RIGHT按钮的第一个图像选择)。 The else statement of my declaration does not work however and the active class is assigned to whatever element is next in line in the DOM. 但是,我的声明的else语句不起作用,并且将活动类分配给DOM中下一行的任何元素。

HTML: HTML:

    <h2>Random Image Gallery</h2>

        <div id="display"></div>

    <div class="active slider" id="slide1">
        <img src="images/482.jpg" alt="random image boats">
        <p>Boats on a River</p>
    </div>

    <div class="slider" id="slide2">
        <img src="images/1109-600x376.jpg" alt="random image">
        <p>Grey Landscape</p> 
    </div>

    <div class="slider" id="slide3">
        <img src="images/1648jpg" alt="random image3">
        <p>River Dock</p>
    </div>

<article>

        <button id="lbutton">Left</button>

        <button id="rbutton">Right</button>

    </article>

javascript (with comments over the conditionals in question): javascript(带有有关条件的注释):

jQuery("#rbutton").on("click", function() {
    "use strict";

    var active = jQuery("div#gallerybox > div.active");
    //"else" conditional is inert?
    var next = active.next("div.slider") ? active.next("div.slider") : jQuery("div#slide1");

    var nextImage = next.find("img").clone().addClass("dis");

    var display = jQuery("div#display");

    var prevDisImg = display.find("img.dis ~ img.dis") ? display.find("img:first-of-type") : null() ;

    nextImage.appendTo("div#display"); 

    prevDisImg.remove();                

    next.addClass("active");

    active.removeClass("active");


});
         //This button works
jQuery("#lbutton").on("click", function() {
    "use strict";

    var active = jQuery("div#gallerybox > div.active");

    var previous = active.prev("div.slider").length ? active.prev("div.slider") : jQuery("div#gallerybox > div:last");

    var prevImage = previous.find("img").clone().addClass("dis");

    var display = jQuery("div#display");

    var nextDisImg = display.find("img.dis + img.dis") ? display.find("img:last-of-type") : null();

    prevImage.appendTo("div#display");

    nextDisImg.remove();

    previous.addClass("active");

    active.removeClass("active");

});

Where am I going wrong with my "else" declarations? 我的“其他”声明在哪里出问题? Or am I going about implementing conditionals in the wrong way? 还是我打算以错误的方式实施条件?

jQuery always returns a new jquery Objects, and theyre truthy so youre doing this: jQuery总是返回一个新的jquery对象,它们是真实的,因此您可以这样做:

var result = {} ? "truthy":" objects are never falsy";

You may check for the length property of the jquery collection instead: 您可以改为检查jquery集合的length属性:

var previous = active.prev("div.slider").length ? active.prev("div.slider") : jQuery("div#gallerybox > div:last");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM