Jak napisać element za pomocą javascript? [duplikat]
To pytanie ma już odpowiedź tutaj:
Muszę dodać element onclick zaraz po innym (textarea).
Jak to zrobić?
29
3 answers
Możesz użyć metody appendChild, aby dodać nowy element
HTML
<div id='div'>
<textarea></textarea>
</div>
Javascript
var div = document.getElementById('div');
var newText = document.createElement('textarea'); // create new textarea
div.appendChild(newText); // add it to the div
Wynikowy HTML
<div id='div'>
<textarea></textarea>
<textarea></textarea>
</div>
4
Author: Ibu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-02 22:54:47
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-02 22:54:47
Zaakceptowana odpowiedź będzie działać w tym konkretnym przypadku, ale aby wstawić element po innym, bardziej ogólnie, wykonujemy następujące zadanie:
someElement.parentNode.insertBefore(newElement, someElement.nextSibling);
Gdzie newElement jest elementem, który ma być wstawiony, a someElement jest elementem, po którym ma być wstawiony.
W3C insertBefore metoda Node interface
111
Author: RobG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-30 09:41:54
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-30 09:41:54
outerHTML
Metoda też działa:
allAloneElement.outerHTML+='<p>I am the new element</p>'
7
Author: user3711851,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-11 14:16:26
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-11 14:16:26