当div高度大于50px时,替换网址(window.location)

时间:2019-06-14 13:21:11

标签: javascript jquery html url height

新手在这里。当div #valor高度大于50px时,我尝试使用window.location.replace更改网站的页面/ URL。

详细信息:

此div #valor每次点击都会增加其高度。但是当它至少达到50像素时,我需要将用户重定向到另一个页面。

我尝试了很多不同的方法,但是我知道每种方法都缺少一些东西。我将在下面列出它们以供参考。我确实认为问题出在我构建代码的方式...

var maskHeight = $("#valor").css('height');

if (maskHeight > 50){
 window.location.replace("https://google.com");
}
else {
alert("if I'm here it means it didn't work"); //just testing
}
});

我有很多不同的选项无法使用,但是它们都有以下if语句:

if ($('#valor').height() > 40) {
window.location.replace("https://google.com");
}

我也尝试过这样的事情:

var div = $("#valor").height();
var win = 50;

if (div > win ) {
    window.location.replace("https://google.com");
}

我的第一种方法(此处未列出)没有用,因为它比较了第一次点击时的价值。我只想做这样的事情: 当#valor高度大于/达到50px>更改网址时。

先谢谢您!非常感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您比较一个字符串(例如10px)和一个整数:

var maskHeight = $("#valor").css('height'); // output for example 10px

if (maskHeight > 40){ . //than you compare it if(40px > 40)
 window.location.replace("https://google.com");
}

将您的var更改为:

var maskHeight = $("#valor").height();

好,您编辑了帖子: 您可以向我们展示您的点击处理程序功能吗?

我认为这是正确的做法:

    var maskHeight = $("#valor").height();
    $("#valor").on('click', function()
    maskHeight = maskHeight + 10;
    if (maskHeight > 40){
        window.location.replace("https://google.com");
        }
    else 
       {
        alert("if I'm here it means it didn't work"); //just testing
       }
});