Jump to content
  • 0

Вывод JS в HTML через <div>


upward
 Share

Question

Код я написал, но вот как его вывести в элемент....мысли нет((

Помо :dash: гите...

<html>
<head>
<script language="JavaScript">
var i=1;
function pow(x, n) {
return (n != 1) ? x*pow(x,n-1) : x; // пока n!=1 сводить к n-1\
}
while (i<=7) {
var a = pow(i, 2);
alert('Квадрат '+i+' равен '+ a);
i++;
}
</script>
</head>
</html>

Вот так должно выводить в HTML.

Квадрат 1 = 1

Квадрат 2 = 4

Квадрат 3 = 9

Квадрат 4 = 16

Квадрат 5 = 25

Квадрат 6 = 36

Квадрат 7 = 49

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0


<div id="container"></div>
<script>
(function() {
onload = function() {
for (var i = 1; i < 7; i++) {
document.getElementById('container').innerHTML += '<p>Квадрат числа ' + i + ' = ' + pow(i) + '</p>';
};
};

function pow(n) {
return n *= n;
};
})();
</script>

Как вариант

Edited by web-monster
Link to comment
Share on other sites

  • 0

Мысль первая: alert не умеет писать в документ, значит, надо поискать другие методы вывода.

искал...но не нашел...сюда и обратился


<div id="container"></div>
<script>
(function() {
onload = function() {
for (var i = 1; i < 7; i++) {
document.getElementById('container').innerHTML += '<p>Квадрат числа ' + i + ' = ' + pow(i) + '</p>';
};
};

function pow(n) {
return n *= n;
};
})();
</script>

Как вариант

Работает)Благодарю)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy