Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Как добавить в элемент текстовый узел (https://javascript.ru/forum/misc/29586-kak-dobavit-v-ehlement-tekstovyjj-uzel.html)

teles 04.07.2012 11:28

Как добавить в элемент текстовый узел
 
<html>
<head>
    <title></title>
    <script type="text/javascript">

        function func1 (Id, tag, count, message1, message2, message3) {

            if (document.getElementById (Id).childNodes[3]) {
                while (document.getElementById (Id).childNodes[3]) {
                    document.getElementById (Id).removeChild (document.getElementById (Id).childNodes[3])
                }
            }
            else {
                for (i = 1; i <= count; i++) {
                    var gotElem = document.getElementById (Id);
                    var newElem = document.createElement (tag);
                    newElem.appendChild (document.createTextNode ('message' + i));
                    gotElem.appendChild (newElem);
                }
            }
        }
    </script>
</head>
<body>
<div id="head" onclick="func1('head','p', 3, 'example1','example2','example3')">
    <h3>Head</h3>
</div>

</body>
</html>

вопрос: как сделать, чтобы вместо message1,message2,message3 выводило exapmple1,example2,example3 ?

ksa 04.07.2012 11:53

Цитата:

Сообщение от teles
как сделать, чтобы вместо message1,message2,message3 выводило exapmple1,example2,example3 ?

Полное назначение твоего скрипта не совсем понятно... Но предложу вариант как подправить твой.

<!DOCTYPE html>
<html>
<head>
<!--
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" type="text/css" href="tmp.css" />
-->
<style type="text/css">
</style>
<script type="text/javascript">
function func1 (Id, tag, arr) {
	if (document.getElementById (Id).childNodes[3]) {
		while (document.getElementById (Id).childNodes[3]) {
			document.getElementById (Id).removeChild (document.getElementById (Id).childNodes[3])
		}
	}
	else {
		for (var i = 0; i <arr.length; i++) {
			var gotElem = document.getElementById (Id);
			var newElem = document.createElement (tag);
			newElem.appendChild (document.createTextNode (arr[i]));
			gotElem.appendChild (newElem);
		}
	}
}
</script>
</head>
<body>
<div id="head" onclick="func1('head','p', ['example1','example2','example3'])">
    <h3>Head</h3>
</div>
</body>
</html>

Deff 04.07.2012 11:54

ksa,
Гы - не поспел за Вами
----------------------------
Тады авторским способом:
<html>
<head>
    <title></title>
    <script type="text/javascript">

        function func1 (Id, tag, count,message1, message2, message3) {

            if (document.getElementById (Id).childNodes[3]) {
                while (document.getElementById (Id).childNodes[3]) {
                    document.getElementById (Id).removeChild (document.getElementById (Id).childNodes[3])
                }
            }
            else {
                for (i = 1; i <= count; i++) {
                    var gotElem = document.getElementById (Id);
                    var newElem = document.createElement (tag);
                    newElem.appendChild (document.createTextNode (eval('message'+i)));
                    gotElem.appendChild (newElem);
                }
            }
        }
    </script>
</head>
<body>
<div id="head" onclick="func1('head','p', 3, 'example1','example2','example3')">
    <h3>Head</h3>
</div>

</body>
</html>

teles 04.07.2012 12:17

Ваш метод даже больше подходит для моих целей :)
а для чего надо было вставлять эти строки
<!--
05 <script src="http://code.jquery.com/jquery-latest.js"></script>
06 <link rel="stylesheet" type="text/css" href="tmp.css" />
07 -->
?

Deff 04.07.2012 12:24

teles,
Эт заготовка универсальной страницы ответов

ksa 04.07.2012 12:54

Цитата:

Сообщение от teles
а для чего надо было вставлять эти строки

- А почему вы так на меня смотрите?
- Ты у меня уже пятый...
(с) к/ф "В бой идут одни старики"


Часовой пояс GMT +3, время: 12:16.