因此,我在实际将数据正确加载到新的 HTML 页面时遇到了问题。目标是当单击标签时,会打开一个新页面,其中包含相应标签的数据。该项目使用的是D3。
我正在关注instructions given in this question ,但是当我单击标签时(假设它有两个结果),我得到 [object Object],[object Object]
而不是实际数据本身。我是否缺少输出实际数据的步骤?
父级中的代码:
label.on('click', function(){
var OpenWindow = window.open("results.html", "mywin", '');
OpenWindow.dataFromParent = results;
OpenWindow.init();
});
子代码
<script type="text/javascript">
var dataFromParent;
function init() {
document.write("<p>" + dataFromParent + "</p>");
}
init();
</script>
任何帮助将不胜感激!谢谢!
最佳答案
从评论中你说你想显示 JSON 对象的内容。
你拥有的是
document.write("<p>" + dataFromParent.toString() + "</p>");
你想要的是
document.write("<p>" + JSON.stringify(dataFromParent) + "</p>");
现在,在父窗口和子窗口中调用 init 是没有意义的。您需要意识到子窗口需要时间来加载,因此从打开器调用该方法,它可能不存在,设置值也是如此。
此外,document.write()
也是一个坏主意。
关于Javascript - window.open 无法正确显示数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26721249/