2015-03-26 1 views
-1

Мне нужно создать объект document в HTML-страницу, используя javaScript , чтобы выполнить новую HTML-страницу; так: Как создать документ Object в HTML-страницу?

<!DOCTYPE html> 
<html> 
    <head> 
    ... some head tags appened 
    <script> 
     // not problem of with jquery or not 
     function createDocument(){ 
     var ta = document.getElementById('ta'); 
     var targetElement = document.getElementById('targetElement') 
     // so i need to know how can use [ta.value] 
     // to execute it in new DOCUMENT Element appended it in [targetElement] 
     // without get or set global variables in origin document 
     } 
    </script> 
    </head> 
    <body> 
    <textarea id="ta"></textarea> 
    <div id="targetElement"> 
     <!-- need to append new html document HERE! --> 
    </div> 
    <!-- 
     when click the run Button the value of text area can be 
     running content to create new document object into targetElement 
    --> 
    <button onclick="createDocument()">RUN</button> 
    </body> 
</html> 
+0

'createElementById'? Вы действительно смешиваете 'createElement' с' getElementById'? – undefined

+0

попробуйте что-то вроде 'var ta = (document.createElement (" div ")) setAttribute (" id "," ta ");' – Downgoat

+0

ops sorry yeah, я имею в виду 'getElementById' yeah – imel

ответ

0

Вы, наверное, перепутали .getElementById и .createElement

<!DOCTYPE html> 
<html> 
    <head> 
    <script> 
     function createDocument(){ 
     var ta = document.getElementById('ta').value; 
     var targetElement = document.getElementById('targetElement'); 
     targetElement.innerHTML = eval(ta); 
     } 
    </script> 
    </head> 
    <body> 
    <textarea onchange="createDocument();" id="ta"></textarea> 
    <div id="targetElement"> 
     <!-- Content goes here --> 
    </div> 
    </body> 
</html> 
+0

таким образом сохранить глобальные переменные, если я использую в '