0

我是 javascript 新手,并试图在新窗口中显示图像。代码是 -

<script type="text/javascript">
    function myfunc(){
        var new_window = window.open();
        var doc = new_window.document;
        var image = document.createElement('img');
        image.setAttribute('src','imagepath');
        image.setAttribute('alt',"image");
        doc.appendChild(image);
    }
</script>

显示“alt”文本时,此图像未在 FireFox 和 Chrome 中显示。在 IE 中没有显示任何内容。请帮忙。

4

3 回答 3

2

您不能将节点插入到 中document,您需要指定body

var doc = new_window.document.body;

修正后对我来说效果很好。

于 2013-09-26T06:06:31.740 回答
0

希望这可以帮助 !!

Js小提琴

<button>Working !!</button>

$('button').off('click').on('click', function(){
        var new_window = window.open();
        var doc = new_window.document.body;
        var image = document.createElement('img');
        doc.appendChild(image);        
       $(doc).find('img').attr({
        src: "http://profile.ak.fbcdn.net/hprofile-ak-ash4/276993_573451729347425_460197233_q.jpg",
             title: "Image" 
});

});
于 2013-09-26T06:32:03.447 回答
0

我发现dosent工作也没有更新版本......

尝试这个

var img = new Image(1,1); //width, height values are optional parameter acc to your requirments

img.src = 'your image url';

希望这会奏效

它在 chrome 中对我有用,但在 Firefox 中未测试,但我认为它也可以在 Firefox 中使用。

于 2013-09-26T06:37:27.867 回答