为什么这个工具提示在IE中不起作用?错误:预期对象?

时间:2012-02-15 21:09:13

标签: javascript jquery internet-explorer object-expected

你能告诉我这段代码我错在哪里:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>tooltip</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

$(document).ready(function() {

        xOffset = 140;
        yOffset = 50;

    $("a.preview").hover(function(e){
        this.t = this.title;
        this.title = "";    
        var c = (this.t != "") ? "<br/>" + this.t : "";
        var mainImage = $(this).find("img").attr("src");
        mainImageWidth = $(this).find("img").width();

        $("body").append("<p id='preview-img'><img src='"+ mainImage +"' alt='Loading...' />"+ c +"</p>");                               
        $("#preview-img")
            .css("top",(e.pageY) + "px")
            .css("left",(e.pageX + (mainImageWidth + yOffset)) + "px")
            .fadeIn("normal");              
    },
    function(){
        this.title = this.t;    
        $("#preview-img").fadeOut("fast");
        $("#preview-img").remove();
    }); 
    $("a.preview").mousemove(function(e){
        $("#preview-img")
            .css("top",(e.pageY) + "px")
            .css("left",(e.pageX + (mainImageWidth + yOffset)) + "px");
    });     

});
</script>
<style>
#preview-img {
    position: absolute;
    border: 5px solid #6d6d6d;
    background: #eaeaea;
    padding: 8px;
    display: none;
    color: #6d6d6d;
    text-align: center;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
}
a.preview img {
    display:none
}
</style>

</head>

<body>
<ul>
    <li>
        <a href="#" class="preview" title="Test 1">Test 1
            <img src="http://images-3.findicons.com/files/icons/2117/nuove/128/camera_test.png" alt="Test 1">
        </a>
    </li>
    <li>
        <a href="#" class="preview" title="Test 2">Test 2
            <img src="http://images-3.findicons.com/files/icons/2117/nuove/128/camera_test.png" alt="Test 2">
        </a>
    </li>
    <li>
        <a href="#" class="preview" title="Test 3">Test 3
            <img src="http://images-3.findicons.com/files/icons/2117/nuove/128/camera_test.png" alt="Test 3">
        </a>
    </li>
    <li>
        <a href="#" class="preview" title="Test 4">Test 4
            <img src="http://images-3.findicons.com/files/icons/2117/nuove/128/camera_test.png" alt="Test 4">
        </a>
    </li>

</ul>

</body>
</html>

它适用于FF,Chrome,Opera但在IE 8中我收到错误:预期的对象。根据我在此处和Internet上的内容,当定义了两个相同的ID时会发生此错误。但我没有看到相同的ID:))

1 个答案:

答案 0 :(得分:0)

在外面定义var mainImageWidth;似乎可以解决错误。我在IE 8中测试了它。

jsFiddle