【问题标题】:Showing the image in tooltip在工具提示中显示图像
【发布时间】:2012-05-29 20:01:44
【问题描述】:

我正在使用tipsy plugin



如果有人将鼠标悬停在他的链接上,我想显示 用户 的图像。现在该链接有一个 id,图像通过该 id 存储在服务器中。例如,如果 id4 那么图像将是 4.jpg 所以我使用这段代码来显示工具提示: p>

$('.tipsy').tipsy({
        html: true,
        gravity : 'e',
        title: function() {
            var i=0;
            alert(++i + "   first");
            var imgPath='<img height="50" width="50" src="dp/' + $(this).attr('id') + '.jpg" />';
            try{
                imgPath=$(imgPath).error(function() {
                    alert(++i + "  middle");
                    //imgPath='<img height="50" width="50" src="dp/no-image.jpg" />';
                });
            }catch(err){
                alert('inside catch');
            }
            alert(++i + "   last");
            return imgPath; 
        }
    });

imgPath 永远不会出现 但从技术上讲如果 error 存在,则将采用 noimage.jpg 的值,但每次它都会向我显示带有 src 的 img 为 .jpg

还有一件事我注意到了......没有错误时的顺序是:

  1. alert(++i + "first");
  2. alert(++i + "last");

    以及出现错误时的顺序:

    1. 警报(++i + "第一");
    2. 警报(++i + "最后");
    3. alert(++i + "middle");

这是错误的,因为 middle 警报介于 firstlast

之间

【问题讨论】:

  • 您的 imgPath 是一个 HTML 字符串。为什么要尝试将其放入 jQUery 对象中?
  • 因为我想知道该图像是否存在于服务器中......所以这就是为什么将它放入Jquery对象中调用error()
  • 好的,我明白了。您是否尝试删除警报并将其更改为 console.log()?有时警报会导致奇怪的同步问题
  • 其 FIRST、LAST、MIDDLE 的顺序相同,这是问题所在,因为 LAST 在 MIDDLE 之前执行,因此 imgPath 未更新

标签: jquery html tooltip


【解决方案1】:

我真的不知道为什么没有捕获异常。但是对于工具提示上的任何动态内容,您可以尝试这些插件:

http://craigsworks.com/projects/simpletip/

http://jquery.bassistance.de/tooltip/demo/

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    您的错误函数似乎由于某种原因被异步调用。我不确定为什么会发生这种情况,可能它可能在 jquery 文档中,但你可以做的是使用 ajax() 函数强制同步检查

    alert("first");
    var imgPath; //not setting imgPath here
            try{ 
                //using ajax() instead of earlier code
                $.ajax({
                    url: 'dp/' + $(this).attr('id') + '.jpg', //might need to give absolute path instead of relative one
                    type: 'get',
                    async:false, //making sync call to ensure this code is executed
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        imgPath='no-image.jpg'; //if image doesnt exists then set no-image
                        alert("middle-error");
                    },
                    success: function(data){
                        imgPath='dp/' + $(this).attr('id') + '.jpg'; //succes -set required                    
                        alert("middle-success");
                    }
                });
            }catch(err){ 
                alert('inside catch'); 
            } 
    alert(" last"); 
    

    我们在这里做的是检查图像,例如dp/4.jpg 存在于服务器上。我们还通过设置 async:false 来强制 sync 这次调用

    【讨论】:

    • 尝试在错误函数中警告 $(this).attr('src') 看看它返回什么
    • 它返回我想要的,但问题是调用序列 FIRST LAST MIDDLE 这是主要问题
    • 好吧,错误函数似乎由于某种原因异步运行。在这种情况下,我上面的代码不成立。您将需要以某种方式强制同步执行 try 块。等我开会回来后,我会试着找到答案。希望你在那之前得到答案:)
    • 我已经更新了我的答案。我正在使用同步 ajax() 调用而不是错误函数来检查服务器上是否存在图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多