Home > Backend Development > PHP Tutorial > js获取一个document.write()创建的对象的有关问题

js获取一个document.write()创建的对象的有关问题

WBOY
Release: 2016-06-13 13:51:57
Original
1465 people have browsed it

js获取一个document.write()创建的对象的问题
document.write("

 
");
var obj = document.getElementById('objDiv');

这样是正确的
但把 去掉后为什么obj得不到对象呢?
document.write("
");
var obj = document.getElementById('objDiv');

------解决方案--------------------
HTML code



<script>

document.write(" <div   id='objDiv'> ");
var   obj   =   document.getElementById('objDiv');
alert(obj)
</script>
<br><font color="#e78608">------解决方案--------------------</font><br>可以不用document.write;<br><br><br>//....原有内容<br><br>//以下代码需要放在最底下。<br><script type="text/javascript"><br />var $ = function(el){return document.getElementById(el);}<br />var $body = document.body;<br /><br />var el = document.createElement('div');<br />el.id = 'someid';<br />el.innerHTML = '内容....<span>其他....';<br />el.className = 'someCssClass';//样式表class名称<br />$body.appendChild(el); //这里附加到document.body上去<br /><br />alert($('someid')); //return HTMLElement.<br /></script><br> <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template