简单的全局变量问题 [英] simple global variables question
问题描述
我有一个非常简单的html页面,其中包含一个全局变量,一个函数
来增加它的值,以及一个调用该函数的表单。然而,
会发生的是每次我点击图片时,页面都会被重新加载,并且值被重置为1.这就是我应该
期待什么?全局变量是否在重载期间不持久?如果
就是这种情况,我如何设置它以便单击按钮
不会自动重新加载页面,而只是调用
功能。这太令人讨厌了:(
< html>
< head>
< script language =" javascript">
var a = 1;
函数incValue()
{
a = a + 5;
}
< / script>
< / head>
>
< body>
< form>
< input type =" image" src =" one.gif" onclick =" incValue()" />
< / form>
< / body>
< / html> ;
lost hope写道:
(剪辑)
< blockquote class =post_quotes> ...是全局变量在重载时不是持久的吗?
Nope。http是无状态的,是否重新加载。
如果这样是这样的,如何设置它以便单击按钮
不会自动重新加载页面,而只是调用
函数。这是如此的愚蠢: (
(剪辑)
< input type =" image">是一个图形提交按钮。为避免形式提交和加载新文件,请尝试:
< a href ="#" onclick =" return incValue()">< img src =" one.gif" />< / a>
var a = 1;
//观看标题栏!
文件。 title =''a =''+ a;
函数incValue()
{
a + = 5;
document.title =''a =''+ a;
返回false; // kill link default
}
" RobB" < FE ****** @ hotmail.com>在消息中写道
news:11 ********************** @ l41g2000cwc.googlegr oups.com ...失去了希望写道:
(剪辑)
...是全局变量在重载时不是持久的吗?
没有。 http是无状态的,是否重新加载。
如果是这种情况,我如何设置它以便单击按钮
不会自动重新加载页面,而只是调用
函数。这是如此令人费解:(
(剪辑)
< input type =" image">是一个图形提交按钮。避免形式<提交和加载新文件,请尝试:
< a href ="#" onclick =" return incValue()">< img src =" ; one.gif" />< / a>
var a = 1;
//观看标题栏!
document.title =''a =' '+ a;
函数incValue()
{
a + = 5;
document.title =''a =''+ a;
返回false; // kill link default
}
对我来说,只需使用< img src =" image.gif" onclick =" incValue()" ;>已经工作了。
图像不是链接或表单输入,但它仍然响应onclick。我
不知道是否但它适用于IE以外的浏览器。
Tony写道:" RobB"< fe ****** @ hotmail.c om>写在消息中
新闻:11 ********************** @ l41g2000cwc.googlegr oups.com ...失去了希望写道:
(剪辑)
...是不是在重新加载时不会持久的全局变量?
没有。 http是无状态的,是否重新加载。
如果是这种情况,我如何设置它以便单击
按钮不会自动重新加载页面,而只是调用
函数。这是如此令人费解:(
(剪辑)
< input type =" image">是一个图形提交按钮。避免形式<提交和加载新文件,请尝试:
< a href ="#" onclick =" return incValue()">< img src =" ; one.gif" />< / a>
var a = 1;
//观看标题栏!
document.title =''a =' '+ a;
函数incValue()
{
a + = 5;
document.title =''a =''+ a;
返回false; // kill link default
}
对我来说,只需使用< img src =" image.gif" onclick =" incValue()">
工作。图像不是链接或表单输入,但它仍然响应
onclick。我不知道它是否适用于浏览器但是除了IE之外。
没关系,图像对象也有onclick处理程序,几乎所有
curr但是,浏览器可能不是一个好习惯,因为它们不会像链接一样被蜘蛛抓住。添加style =" cursor:pointer;"对于
更像链接的外观。
hi, i have a very simple html page with a global variable, a function
to increment its value, and a form that calls the function. however,
what happens is that every time i click the image, the page is
reloaded, and the value is reset to 1. is this what i should be
expecting? are global variables not persistent across reloads? if
this is the case, how can i set it up so that clicking the button
doesn''t reload the page automatically, but rather just calls the
function. this is so frusterating :(
<html>
<head>
<script language="javascript">
var a=1;
function incValue()
{
a = a+5;
}
</script>
</head>
<body>
<form>
<input type="image" src="one.gif" onclick="incValue()"/>
</form>
</body>
</html>
lost hope wrote:
(snip)
...are global variables not persistent across reloads?
Nope. http is stateless, reload or not.
if this is the case, how can i set it up so that clicking the button
doesn''t reload the page automatically, but rather just calls the
function. this is so frusterating :(
(snip)
<input type="image"> is a graphical submit button. To avoid form
submission and the loading of a new document, try:
<a href="#" onclick="return incValue()"><img src="one.gif" /></a>
var a = 1;
//watch that title bar !
document.title = ''a = '' + a;
function incValue()
{
a += 5;
document.title = ''a = '' + a;
return false; //kill link default
}
"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...lost hope wrote:
(snip)...are global variables not persistent across reloads?
Nope. http is stateless, reload or not.if this is the case, how can i set it up so that clicking the button
doesn''t reload the page automatically, but rather just calls the
function. this is so frusterating :(
(snip)
<input type="image"> is a graphical submit button. To avoid form
submission and the loading of a new document, try:
<a href="#" onclick="return incValue()"><img src="one.gif" /></a>
var a = 1;
//watch that title bar !
document.title = ''a = '' + a;
function incValue()
{
a += 5;
document.title = ''a = '' + a;
return false; //kill link default
}
For me, just using <img src="image.gif" onclick="incValue()"> has worked.
The image isn''t a link or form input, but it still responds to "onclick". I
don''t know if it works in browsers other than IE, though.
Tony wrote:"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...lost hope wrote:
(snip)...are global variables not persistent across reloads?
Nope. http is stateless, reload or not.if this is the case, how can i set it up so that clicking the button doesn''t reload the page automatically, but rather just calls the
function. this is so frusterating :(
(snip)
<input type="image"> is a graphical submit button. To avoid form
submission and the loading of a new document, try:
<a href="#" onclick="return incValue()"><img src="one.gif" /></a>
var a = 1;
//watch that title bar !
document.title = ''a = '' + a;
function incValue()
{
a += 5;
document.title = ''a = '' + a;
return false; //kill link default
}
For me, just using <img src="image.gif" onclick="incValue()"> has
worked. The image isn''t a link or form input, but it still responds to "onclick". I don''t know if it works in browsers other than IE, though.
That''s ok, Image objects have onclick handlers too, in almost all
current browsers. Probably not a good habit to get into, however, as
they won''t be spidered as links are. Add style="cursor:pointer;" for a
more link-like appearance.
这篇关于简单的全局变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!