【问题标题】:Replace url (window.location) when div height gets bigger than 50px当 div 高度大于 50px 时替换 url (window.location)
【发布时间】:2019-06-14 21:41:36
【问题描述】:

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

详情:

此 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 height 大于/达到 50px > 更改 url。

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

【问题讨论】:

  • 那么代码只在第一次调用时运行。如果元素正在被其他东西调整大小,它将不会被调用。那么当它改变时你会以某种方式调用它吗?

标签: javascript jquery html url height


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 2018-04-30
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2014-01-09
    • 2019-06-23
    相关资源
    最近更新 更多