简体   繁体   English

onclick javascript函数不适用于Firefox / IE

[英]onclick javascript function does not work with Firefox/IE

I have a problem with my Captcha reload action. 我的验证码重新加载操作有问题。 It works very well with Chrome but not with Firefox or IE. 它与Chrome搭配使用非常好,但与Firefox或IE搭配却无法。 There is a Captcha and a Button and I want to refresh the Captcha without reloading the entire page. 有一个验证码和一个按钮,我想刷新验证码而不重新加载整个页面。 I would be happy if I could get some help with this. 如果能对此有所帮助,我将非常高兴。 head with javascript code: 用javascript代码开头:

<?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>

captcha and refresh button: 验证码和刷新按钮:

<img src="resources/templates/captcha/Captcha.php" id='captcha'>
    <a href="#" onclick='javascript:reloadCaptcha();'>
        <img src="../backend/resources/media/captcha_refresh.png">
    </a>

I suspect the problem might be caching. 我怀疑问题可能在缓存中。 You can fix this by adding a timestamp to your new img src , like this: 您可以通过在新的img src添加时间戳来解决此问题,如下所示:

function reloadCaptcha() {
    var img = document.getElementById('captcha'),
        timestamp = new Date().getTime();
    img.src = 'resources/templates/captcha/Captcha.php?' + timestamp;
 }

Your syntax is bad. 您的语法错误。

Option 1: 选项1:

<a href="javascript:reloadCaptcha();">

Option 2: 选项2:

<a href="#" onclick='reloadCaptcha();'>

Option 3: use the EventHandler API. 选项3:使用EventHandler API。

As well as avoiding caching you need to return false in the onclick 除了避免缓存外,您还需要在onclick中返回false

<a href="#" 
onclick='reloadCaptcha(); return false'><img 
src="../backend/resources/media/captcha_refresh.png"></a>

I got the same problem on Firefox and Internet Explorer. 我在Firefox和Internet Explorer上遇到了同样的问题。 I used event handler and I solved my problem. 我使用了事件处理程序,并且解决了我的问题。 Use 采用

evt.preventDefault(); 
return false;

In your javascript functions. 在您的javascript函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM