gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:03:37 27 4
gpt4 key购买 nike

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

详细信息:

此 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 > 更改 url。

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

最佳答案

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

关于javascript - 当 div 高度大于 50px 时替换 url (window.location),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56599095/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com