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

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

WBOY
WBOYOriginal
2016-06-13 13:51:571444browse

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>
Statement:
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
Previous article: 模板。该怎么解决 Next article: php linux解决方案