如何在<div>标签中添加raphaeljs对象

Har*_*ari 8 javascript jquery raphael

$(document).ready(function(){                           
    $("#btnAO").live("click", function(){
        $("#canvasdiv").append("<div id='id1' width='50px' height='50px'></div>");
            $("#id1").append(new Raphael(document.getElementById('canvasdiv'), 900, 600).rect(30, 50, 80, 100).attr({
                fill : "blue",
                stroke : "black",
                strokeWidth : 0,
                r : 5
        }));
    });
});
Run Code Online (Sandbox Code Playgroud)

我试过这个添加Raphael对象,但它不会显示在屏幕上

Sup*_*upr 18

拉斐尔渲染到你给它作为第一个参数的容器中.返回值是用于渲染的Raphael纸质对象.简而言之,只是切掉$("#id1").append它,它出现了.

$(document).ready(function(){                           
    $("#btnAO").live("click", function(){
        $("#canvasdiv").append("<div id='id1' width='50px' height='50px'></div>");
        var paper = new Raphael(document.getElementById('canvasdiv'), 900, 600);
        paper.rect(30, 50, 80, 100).attr({
            fill : "blue",
            stroke : "black",
            strokeWidth : 0,
            r : 5
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

进一步,因为你使用jQuery无论如何,你可能要替换document.getElementById('canvasdiv')$('#canvasdiv').get(0)一致性.

  • 嗨@HarshalChauhari,你可以点击左边的勾号来接受这个答案吗? (5认同)