jquery在div上显示图像

时间:2012-07-23 17:07:56

标签: javascript jquery

我再次在这里发帖,我似乎找不到解决这个问题的方法。我使用jquery和jstree插件来构建一个树,并基于jstree的node_id,我在div上显示一些图像。我面临的问题是,有些图像在它们存在时不会加载。当我遇到这个问题时,我通过检查图像的宽度来验证所讨论的图像实际上没有加载。下面是代码。如何在显示之前确保加载图像。再次,任何对此的帮助都非常感激,因为我对jquery很新:

(document).ready(function () {
    $("#div1").jstree({
        "xml_data": {
            "ajax": {
                "url": "tree.xml"
            },
            "xsl": "nest"
        },
        "plugins": ["themes", "xml_data", "ui", "types"]
    }).bind("select_node.jstree", function (event, data) {
        var node_id = data.rslt.obj.attr("id");
        if (node_id.indexOf("TEAM") >= 0) var teamA;
        var teamB;
        teamA = "image/" + node_id + "-scores.png";
        teamB = "image/" + node_id + "-standing.png";
        img1 = new Image;
        img1.src = teamA;
        img2 = new Image;
        img2.src = teamB;
        $("#div2").html(img1);
        $("#div3").html(img2);
    });

2 个答案:

答案 0 :(得分:1)

$.html采用HTML字符串。调用$.append代替节点或jQuery对象

    $("#div2").append(img1);
    $("#div3").append(img2);

答案 1 :(得分:0)

你可以使用jQuery.html,但你必须提供一个字符串(比如'')而不是Image对象。 您可以尝试将图片对象转换为字符串,也许您会发现此链接很有用:http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx