0

我正在使用 jQuery 1.2.6 :(

当用户单击 ID=LINKID 并具有锚标记的 Html 元素时,他将被重定向到新页面。我想确保在浏览器移动到另一个页面之前获取一个小图像。

有没有办法在jQuery中使用回调或任何其他机制。

HTML:
<div id="LINKID"><a href="link location"> </a></div> 


SCRIPT:
jQuery("#LINKID a").click(function(){
var url = "Image location";
(new Image()).src = url;
});

如果我在上面的代码片段之后添加这样的 hack。

jQuery("#LINKID a").click(function() { return false; } );

我可以看到正在获取的图像,但这将停止与标签相关的功能

4

2 回答 2

1

尝试这个

jQuery("#LINKID a").click(function(){
   var url = "Image location";

   jQuery('<img />')
   .appendTo(document.body)
   .load(function(){
        //Once the image is loaded unbind the click even handler and
        //then let the link follow its url
        jQuery("#LINKID a").unbind('click')[0].click();
   })
   .attr('src', url);
});
于 2012-01-20T19:21:25.343 回答
0

这个适用于所有浏览器。必须在网址中添加时间戳

jQuery("#LINKID a").click(function() {            

var url = "Image Location";
    url += "&_=" + (+new Date());

var img = new Image();

img.onload = function() {

             window.location = jQuery(this).attr("href");

        };

   img.src = url;    

   return false;

});
于 2012-01-25T04:04:52.600 回答