2012-09-27 24 views
1

我有这样的代码:HTML写入新窗口中打开的问题

<div class="col3"> 
    <a id = "training-launch-button" href="javascript:void(0);" title=" My title here" class="button" onClick="Test();">Launch</a> 
</div>  

    function Test() { 
     var new_window= window.open('','Ratting','width=550,height=170,0,status=0,resizable=1'); 
     new_window.document.createElement("div"); 
     document.getElementsByTagName('div')[0].innerHTML = '<ol><li>html data</li></ol>';  

    } 

东西是不正确的,我没有看到有序列表项? 我最终想在新窗口中构建一些HTML。

+0

看看创建页面的来源 – CosminO

+1

getElementsByTagName返回一个包含多个元素的数组,ocument.getElementsByTagName('div')[0] .innerHTML可能会更好。或者只使用id和getElementById。 – fijter

+0

用chrome打开它并右击 - > inspectElement,检查错误并在这里粘贴相关错误请 – pythonian29033

回答

0

使用此JS

function Test() { 
    var newWindow= window.open('','Ratting','width=550,height=170,0,status=0,resizable=1'); 
    var newContent = "<HTML><HEAD><TITLE>One Sub Window</TITLE></HEAD>"; 
    newContent += "<BODY><div><ol><li>html data</li></ol></div>"; 
    newContent += "</BODY></HTML>"; 
    newWindow.document.write(newContent); 
    newWindow.document.close(); 
} 
+0

为什么使用'createAttribute(“div”)'创建'div'而不是'createElement()'? – 2012-09-27 12:14:30

+0

感谢所有我改变了new_window.document.getElementsByTagName('div')[0] .innerHTML和new_window.document.createElement(“div”);但仍然没有,我右键单击并查看源和我所有的是? –

+0

我完全更新了我的代码并进行了检查。它完美的工作。 –

0

我会尝试

new_window.document.getElementsByTagName('div')[0].innerHTML = ...

0

我觉得这是你的问题; getElementsByName返回一个数组,而不是一个元素,所以;

new_window.document.getElementsByTagName('div')[0].innerHTML = '<ol><li>html data</li></ol>'; 

注:我有一个 '[0]' 在那里

0

这应做到:

var new_window= window.open('','Ratting','width=550,height=170,0,status=0,resizable=1'); 
var div = new_window.document.createElement('div'); 
new_window.document.body.appendChild(div); 
div.innerHTML = '<ol><li>html data</li></ol>'; 
0

你实际上没有追加新的div到新文档的身体,你, “将不得不使用.appendChild()方法是,看到这一点:

function Test() { 
    var new_window = window.open('','Ratting','width=550,height=170,0,status=0,resizable=1'); 
    var div = new_window.document.createElement("div"); 
    new_window.document.getElementsByTagName('body')[0].appendChild(div); 
    div.innerHTML = '<ol><li>html data</li></ol>';   
} 

see here - working example