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



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

详:

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

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

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 时>更改网址。

提前谢谢你!任何帮助都非常感谢。

您将字符串与(例如)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
}
});

最新更新