javascript - 在新打开的窗口中访问元素

标签 javascript jquery html

我想用 JavaScript 打开一个 html 文件并将一些数据写入其中的一些元素。我尝试了以下方法:

var info_window = window.open('info_print.html')
info_window.document.getElementById('new_info').innerHTML = data;

但是当我有 #new_info 元素时我得到了这个错误:

cannot set property 'innerHTML' of null

我该怎么办? 我检查了评论的问题,但它不是我的问题。

最佳答案

假设您的 info_print.html 实际上包含您要查找的元素,那么您需要等待 Window 加载它,然后再访问它。

这是一个使用 onload 事件的简单解决方案:

var info_window = window.open('info_print.html');

info_window.addEventListener('load', function() {
    info_window.document.getElementById('new_info').innerHTML = data;
});

关于javascript - 在新打开的窗口中访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53319401/

相关文章:

Javascript:RegExp 土耳其字符问题

jQuery 在网页上显示日期

javascript - 使移动页面始终在顶部加载

javascript - 表格中与文本一起内嵌 D3 迷你图

javascript - Colorbox 调整大小不起作用,返回 "TypeError: $.fn.colorbox is undefined"

javascript - html表格的多个过滤器

html - 切断具有动态高度的元素底部

html - zurb foundation 将一行分成两列

javascript - Angular Controller 继承范围问题

javascript - Bootstrap Carousel 的多个轮播指示器