抱歉,我是JS的新手。 我有一个基本的html页面,我在其中放了一张幻灯片,每次单击都会使幻灯片前进。 我想发生的事情是,当用户单击下一步并位于最后一张幻灯片上时,将其切换到新页面。 我的html:

<div id="images">
    <img id="image1" src="http://www.lorempixel.com/960/580/sports" />
    <img id="image2" src="http://www.lorempixel.com/960/580/cats" />
    <img id="image3" src="http://www.lorempixel.com/960/580/food" />
    <img id="image4" src="http://www.lorempixel.com/960/580/people" />
</div>

我的JS看起来像这样:

var max = 4;

function goToNext() {
    var hash = String(document.location.hash);
    if (hash && hash.indexOf(/image/)) {
        var newh = Number(hash.replace("#image", ""));
        (newh > max - 1) ? newh = 0 : void(null);
        document.location.hash = "#image" + String(newh + 1);
    } else {
        document.location.hash = "image1";
    }
}

function goToPrevious() {
    var hash = String(document.location.hash);
    if (hash && hash.indexOf(/image/)) {
        var newh = Number(hash.replace("#image", ""));
        (newh == 1) ? newh = 5 : void(null);
        document.location.hash = "#image" + String(newh - 1);
    } else {
   var url = window.location.href;

if (url.search("#image") > 0) {
    window.location.href = "strategy.html";
  }    
 }
}

===============>>#1 票数:0 已采纳

改变这个:

(newh > max - 1) ? newh = 0 : void(null);
        document.location.hash = "#image" + String(newh + 1);

对此:

(newh > max - 1) ? window.location.href = "strategy.html" :
        document.location.hash = "#image" + String(newh + 1);

jsfiddle

===============>>#2 票数:0

只要检查newh是否为零,

    if(newh == 0) 
        document.location.href = "strategy.html";

这是所有代码, DEMO

var max = 4;

function goToNext() {
    var hash = String(document.location.hash);
    if (hash && hash.indexOf(/image/)) {
        var newh = Number(hash.replace("#image", ""));
        (newh > max - 1) ? newh = 0 : void(null);

        if(newh == 0) 
            document.location.href = "strategy.html";
        else 
            document.location.hash = "#image" + String(newh + 1);
    } else {
        document.location.hash = "image1";
    }
}

function goToPrevious() {
    var hash = String(document.location.hash);
    if (hash && hash.indexOf(/image/)) {
        var newh = Number(hash.replace("#image", ""));
        (newh == 1) ? newh = max+1 : void(null);
        document.location.hash = "#image" + String(newh - 1);
    } else {
        document.location.hash = "image4";    
 }
}

  ask by user1888521 translate from so

未解决问题?本站智能推荐: