我希望有人包含我的JS文件,它做了一些整洁的事情.我认为我就像谷歌一样把它放了:你在源代码中加了一个脚本标签,google js会处理所有事情.在我的php文件中是这样的:
echo '<script type="text/javascript"
src="http://www.site.com/gadget.js">
</script>';
Run Code Online (Sandbox Code Playgroud)
我是gadget.js,我写了一个范围:
document.write('<span id="GadgetPicture>');
document.write('</span>');
window.setTimeout('refreshImage();', 2000);
Run Code Online (Sandbox Code Playgroud)
在refreshImage函数中,我想引用我的span:
document.getElementById("GadgetPicture");
Run Code Online (Sandbox Code Playgroud)
但它让我无效......有人知道为什么吗?
另外,document.getElementById("body")
也给null.
米歇尔
你错过了一个引用:
document.write('<span id="GadgetPicture">')
Run Code Online (Sandbox Code Playgroud)
使用DOM方法(createElement,appendChild)通常比使用document.write更好,但它仍然可以工作.