在javascript中使用减号 [英] using a minus in javascript

查看:97
本文介绍了在javascript中使用减号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下链接,根据屏幕分辨率

显示一个带有数字的警报。但不是显示一个数字''NaN',而是显示




< a href =" javascript :警告(''你的理想估计身高是''+

screen.height * 0.55-screen.height * 0.2);" class =" BodyLink">点击计算

height< / a>


但当我删除等式'''屏幕的一部分时.height * 0.2''一个数字是

正确显示。当我在等式中有一个减号时,为什么它不起作用。

我没有使用javascript经验如此借口如果这是一个基本问题


谢谢

ian

I have the following link to bring up an alert with a figure in it based on
the screen resolution. but instead of displaying a number ''NaN'' is
displayed.

<a href="javascript:alert(''Your ideal estimated height is ''+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to calculate
height </a>

But when I remove the part of the equation ''-screen.height*0.2'' a figure is
displayed properly. Why doesnt it work when I have a minus in the equation.
Im not experienced with javascript so excuse if this is a basic question

thanks
ian

推荐答案

使用javascript进行计算的任何好链接也将非常多

赞赏

" mantrid" < ia ******** @ virgin.netwrote in message

news:Wa ***************** @ newsfe2-gui。 ntli.net ...
any good links to doing calculations with javascript would also be very much
appreciated
"mantrid" <ia********@virgin.netwrote in message
news:Wa*****************@newsfe2-gui.ntli.net...

我有以下链接来显示基于
的数字的警报
I have the following link to bring up an alert with a figure in it based



on

on


屏幕分辨率。但不是显示一个数字''NaN',而是显示




< a href =" javascript :警告(''你的理想估计身高是''+

screen.height * 0.55-screen.height * 0.2);" class =" BodyLink">点击
the screen resolution. but instead of displaying a number ''NaN'' is
displayed.

<a href="javascript:alert(''Your ideal estimated height is ''+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to



计算

calculate


height< / a>


但当我删除等式''-screen.height * 0.2''的一部分时,数字
height </a>

But when I remove the part of the equation ''-screen.height*0.2'' a figure




is


正确显示。当我在
displayed properly. Why doesnt it work when I have a minus in the



等式中有一个减号时,为什么它不起作用。

equation.


我没有使用javascript的经验如此借口如果这是一个基本问题


谢谢

ian
Im not experienced with javascript so excuse if this is a basic question

thanks
ian



mantrid说:
mantrid said:

>
我有以下链接根据屏幕分辨率显示带有数字的警报。

< a href =" javascript :alert(''您理想的估计身高),而不是显示数字''NaN'。是''+
screen.height * 0.55-screen.height * 0.2); class =" BodyLink">点击计算
高度< / a>

但当我删除等式''-screen.height * 0.2''的一部分时,一个数字
正确显示。当我在等式中有一个减号时,为什么它不起作用。
我没有经验的javascript这样的借口如果这是一个基本问题
>
I have the following link to bring up an alert with a figure in it based on
the screen resolution. but instead of displaying a number ''NaN'' is
displayed.

<a href="javascript:alert(''Your ideal estimated height is ''+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to calculate
height </a>

But when I remove the part of the equation ''-screen.height*0.2'' a figure is
displayed properly. Why doesnt it work when I have a minus in the equation.
Im not experienced with javascript so excuse if this is a basic question



你是实际上有+的问题运算符,而不是 - 。

您的代码:

1.计算screen.height * 0.55

2.连接该值用你的想法估计高度是。

3.计算screen.height * 0.2

4.尝试从步骤2的字符串结果中减去该值

这不是一个数字。


将你的减法括在括号中,所以在连接之前它将被执行




''你理想的估计身高是''+(screen.height * 0.55-screen.height * 0.2)


你真的不应该''这种方式滥用javascript

伪协议的副作用,但如果必须,你应该有

事件处理程序返回false以防止页面重新加载

每次单击链接时:


href =" javascript :alert (''你理想的估计身高是''+

(screen.height * 0.55-screen.height * 0.2));返回false"

-

You''re actually having trouble with the "+" operator, not the "-".
Your code:
1. calculates screen.height*0.55
2. concatenates that value with "Your idea estimated height is ".
3. calculates screen.height*0.2
4. tries to subtract that value from the string result of step 2
which is Not a Number.

Enclose your subtraction in parentheses, so it will be performed
before the concatenation:

''Your ideal estimated height is ''+(screen.height*0.55-screen.height*0.2)

You really shouldn''t be abusing he side-effect of the javascript:
pseudo-protocol that way, but if you must, you should have the
event handler return false to prevent the page from reloading
each time the link is clicked:

href="javascript:alert(''Your ideal estimated height is ''+
(screen.height*0.55-screen.height*0.2));return false"
--


你的陈述被一个叫做运营商

总统的东西搞糊涂了。尝试将等式的数学部分包装在一个额外的集合中

这样的parens:


alert(''你的理想估计高度是''+( screen.height * 0.55 -

screen.height * 0.2))

强制公式在尝试转换之前评估一个数字

到字符串并将其附加到您的邮件。正如你所写的那样,

等式首先是乘法运算符,然后加上

你的理想......。字符串到第一个数字,使其成为一个字符串。当它试图减去第二个数字时,解析器认为我不能...... / b $ b从字符串中减去一个数字。反而向你扔了NaN。


干杯!


< / bd>

mantrid写道:
Your statement is being confused by something called operator
presidence. Try wrapping the math part of the equation in an extra set
of parens like this:

alert(''Your ideal estimated height is '' + (screen.height * 0.55 -
screen.height * 0.2))
It forces the equation to evaluate a number before trying to convert it
to a string and attach it to your message. As you had it written, the
equation was doing the multiplication operators first, then adding the
"Your ideal...." string to the first number, making it a string. When
it tried to subtract the second number, the parser thought "I can''t
subtract a number from a string" and threw NaN at you instead.

Cheers!

</bd>
mantrid wrote:

使用javascript进行计算的任何好链接也非常多

赞赏


" ; mantrid" < ia ******** @ virgin.netwrote in message

news:Wa ***************** @ newsfe2-gui。 ntli.net ...
any good links to doing calculations with javascript would also be very much
appreciated
"mantrid" <ia********@virgin.netwrote in message
news:Wa*****************@newsfe2-gui.ntli.net...

我有以下链接来显示基于
的数字的警报
I have the following link to bring up an alert with a figure in it based



on

on


屏幕分辨率。但不是显示一个数字''NaN',而是显示




< a href =" javascript :警告(''你的理想估计身高是''+

screen.height * 0.55-screen.height * 0.2);" class =" BodyLink">点击
the screen resolution. but instead of displaying a number ''NaN'' is
displayed.

<a href="javascript:alert(''Your ideal estimated height is ''+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to



计算

calculate


height< / a>


但当我删除等式''-screen.height * 0.2''的一部分时,数字
height </a>

But when I remove the part of the equation ''-screen.height*0.2'' a figure




is


正确显示。当我在
displayed properly. Why doesnt it work when I have a minus in the



等式中有一个减号时,为什么它不起作用。

equation.


我没有使用javascript的经验如此借口如果这是一个基本问题


谢谢

ian

Im not experienced with javascript so excuse if this is a basic question

thanks
ian


这篇关于在javascript中使用减号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆