首页 \ 问答 \ Javascript添加数字就好像它们是一个字符串[重复](Javascript adding numbers as if they were a string [duplicate])

Javascript添加数字就好像它们是一个字符串[重复](Javascript adding numbers as if they were a string [duplicate])

这个问题在这里已有答案:

我第一次玩饼干,我已经完成了部分保存。 我正在保存的数据是数字,这些nubers最重要的部分是我可以添加,减去等等。 但是,当我尝试将一个数字添加到我保存的参数之一时,它会将它们添加为文本。

示例:我有一个名为value的cookie,当我想要这个值时,我使用Jeffery To找到的脚本,如下所示:

function readCookie(name) {
    return (name = new RegExp('(?:^|;\\s*)' + ('' + name).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '=([^;]*)').exec(document.cookie)) && name[1];
}

我收集了这个cookie后,我想添加一个。 让我们说这个值等于9,它应该是这样的: value + 1 = 10 。 简单的数学。 然而它给了我91 。 为什么这样做? 我知道这是因为它认为数字是一串文字,但我怎么能让它像数字一样?

解决方案阅读完评论后,我了解到我需要将我的value放在parseInt() 。 所以我只是修改了功能说:

function readCookie(name) {
        return parseInt((name = new RegExp('(?:^|;\\s*)' + ('' + name).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '=([^;]*)').exec(document.cookie)) && name[1]);
    }

This question already has an answer here:

I have been playing around with cookies for the first time, and I have saving part of it completed. The data I'm saving are numbers and the most important part of these nubers is that I can add, subtract and so on with these. However when I try to add a number to one of my saved parametres it adds them as if they were text.

Example: I have a cookie called value, and when I want this value I use a script I found by Jeffery To that looks like this:

function readCookie(name) {
    return (name = new RegExp('(?:^|;\\s*)' + ('' + name).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '=([^;]*)').exec(document.cookie)) && name[1];
}

After I have collected this cookie I want to add one to it. Lets say that value equals nine, when it should look like this: value + 1 = 10. Simple math. However it gives me this 91. Why does it do this? I know that it is because it thinks the numbers are a string of text, but how can I get this to behave like numbers?

Solution After reading the comments I learned that i needed to put my value inside a parseInt(). So i simply modified the funtion to say:

function readCookie(name) {
        return parseInt((name = new RegExp('(?:^|;\\s*)' + ('' + name).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '=([^;]*)').exec(document.cookie)) && name[1]);
    }

原文:https://stackoverflow.com/questions/40661354
更新时间:2022-07-18 22:07

最满意答案

看起来突然是因为默认情况下它没有顶部的边框,然后在悬停时它突然有顶部边界。 因此,在mouseout中,而不是转换,它的作用是隐藏顶部边框,因为没有要引用的初始值用于转换。

尝试这个:

#container1 >  div.triangle {
    border-bottom: 80px solid red;
    border-top: 0 solid green;
    border-left: 60px solid transparent; 
    border-right: 60px solid transparent;
    width: 0px;
    height: 0px;

   -webkit-transition: all 1.2s ease-in-out;
}

It seems the abruptness is due to the fact that by default it does not have a border on top, then on hover it suddenly has border on top. So in mouseout, instead of transitioning, what its doing is hiding the top border because there was no initial value to reference for transition.

Try this:

#container1 >  div.triangle {
    border-bottom: 80px solid red;
    border-top: 0 solid green;
    border-left: 60px solid transparent; 
    border-right: 60px solid transparent;
    width: 0px;
    height: 0px;

   -webkit-transition: all 1.2s ease-in-out;
}

相关问答

更多
  • 您只将转换应用于:hover伪类,而不是元素本身。 .item { height:200px; width:200px; background:red; -webkit-transition: opacity 1s ease-in-out; -moz-transition: opacity 1s ease-in-out; -ms-transition: opacity 1s ease-in-out; -o-transition: opacity 1s ease-in-ou ...
  • 不是一个理想的解决方案(从纯粹主义者的角度来看),但你是否考虑过将元素包装在另一个元素中并将变换应用到那个元素? Not an ideal solution (from a purist's point of view), but have you considered wrapping the element inside another and just applying the transform to that one?
  • 我在CSS3 Tryit Editor中尝试过以下操作,它在Chrome(12.0.742.60 beta-m)中工作。