onclick javascript函数不适用于Firefox/IE
我的验证码重新加载操作有问题。它在Chrome上运行得很好,但在Firefox或IE上却不行。 有一个验证码和一个按钮,我想刷新验证码而不重新加载整个页面。如果我能得到一些帮助,我会很高兴的。 带有javascript代码的头部:onclick javascript函数不适用于Firefox/IE,javascript,html,button,onclick,reload,Javascript,Html,Button,Onclick,Reload,我的验证码重新加载操作有问题。它在Chrome上运行得很好,但在Firefox或IE上却不行。 有一个验证码和一个按钮,我想刷新验证码而不重新加载整个页面。如果我能得到一些帮助,我会很高兴的。 带有javascript代码的头部: <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<link href='resources/styles.css' rel='stylesheet' type='text/css' />
<title>...</title>
<script language="javascript" type="text/javascript">
function reloadCaptcha()
{
img = document.getElementById('captcha');
img.src = 'resources/templates/captcha/Captcha.php';
}
</script>
</head>
<img src="resources/templates/captcha/Captcha.php" id='captcha'>
<a href="#" onclick='javascript:reloadCaptcha();'>
<img src="../backend/resources/media/captcha_refresh.png">
</a>
...
函数重载captcha()
{
img=document.getElementById('captcha');
img.src='resources/templates/captcha/captcha.php';
}
验证码和刷新按钮:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<link href='resources/styles.css' rel='stylesheet' type='text/css' />
<title>...</title>
<script language="javascript" type="text/javascript">
function reloadCaptcha()
{
img = document.getElementById('captcha');
img.src = 'resources/templates/captcha/Captcha.php';
}
</script>
</head>
<img src="resources/templates/captcha/Captcha.php" id='captcha'>
<a href="#" onclick='javascript:reloadCaptcha();'>
<img src="../backend/resources/media/captcha_refresh.png">
</a>
您的语法不正确
备选案文1:
<a href="javascript:reloadCaptcha();">
备选案文2:
<a href="#" onclick='reloadCaptcha();'>
选项3:使用EventHandler API。以及避免缓存。您需要在onclick中返回false
<a href="#"
onclick='reloadCaptcha(); return false'><img
src="../backend/resources/media/captcha_refresh.png"></a>
我怀疑问题可能是缓存。您可以通过向新的
img src
添加时间戳来解决此问题,如下所示:
function reloadCaptcha() {
var img = document.getElementById('captcha'),
timestamp = new Date().getTime();
img.src = 'resources/templates/captcha/Captcha.php?' + timestamp;
}
我在Firefox和Internet Explorer上遇到了同样的问题。我使用事件处理程序解决了我的问题。使用
evt.preventDefault();
return false;
在javascript函数中。onclick只能运行javascript。使用“javascript:”是多余的。在控制台中,您会遇到什么错误?函数重载captcha()未定义????或者您的脚本无效?请尝试在脚本中包含警报('xxx');要在不需要
javascript:
部分的情况下查看它是否正在运行,它仍应执行。也许浏览器正在缓存图像,因为您没有将img src更改为新的内容。如果您从控制台手动调用该函数是否有效?警报有效,必须与imgyou有关,您需要返回false!千万不要在href中使用javascript:protocol-它会部分卸载页面,并有许多其他缺点这里的语法没有问题。事件处理程序中的javascript:
部分将被忽略。请参阅@mplungjan您有该声明的来源吗?href:javascript肯定不会被忽略!onclick=“javascript:Will你确定@mplungjan?我更喜欢a href=javascript解决方案。不会更改我的url并刷新。谢谢!这有帮助。现在唯一的问题是,如果我点击这个按钮,它不会刷新,但应该是这样的。是的,根据浏览器的不同,时间戳只会每15-60毫秒更新一次。不幸的是,Javascript的内置随机数生成器也有同样的问题。您可以通过添加一些代码来检测重复项来解决此问题,但这可能不是必需的。您对问题的描述很差。您需要避免缓存以刷新,但也要返回false以不跟踪链接抱歉,我不知道问题出在哪里。我试图把我拥有的东西拿出来。无论如何,谢谢你。