您可以onload
在图像上使用该事件,以准确了解您的新验证码图像何时加载到浏览器上,我在您的标记中添加了 ID:
<img id="captcha" src="captcha_img.png">
<a id="reloadLink" href="..">
<img id="reloadImg" src="reload.png"> Reload Captcha Image
</a>
然后在您的代码中,连接事件处理程序:
// Connect event handlers
window.onload = function () {
// get the DOM elements
var captcha = document.getElementById('captcha'),
reloadImg = document.getElementById('reloadImg'),
reloadLink = document.getElementById('reloadLink');
// reload the captcha image when the link is clicked
reloadLink.onclick = function () {
var captchaURL = "captcha.php"; // change this with your script url
captcha.src = captchaURL + "?nocache=" + (+new Date()); // cache breaker
reloadImg.src = "reload.gif"; // set "animated" reload image
return false; // prevent link navigation
};
captcha.onload = function () { // when the captcha loaded, restore reload image
reloadImg.src = "reload.png"; // "static" reload image
};
};