Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   document.getElementById( ).write( ) - почему не работает? (https://javascript.ru/forum/misc/56450-document-getelementbyid-write-pochemu-ne-rabotaet.html)

Дмитрий_Кирсанов 16.06.2015 18:52

document.getElementById( ).write( ) - почему не работает?
 
document.getElementById("td1").write(a1); - Почему не работает?



<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>15</title>
  </head>
  <body>
    <table align="center" bgcolor="#FAEBD7" border="9" bordercolor="#DEB887" height="320px" width="320px">
      <tr align="center">
        <td bgcolor="#F0F8FF" width="80px" id="td1"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
      </tr>
      <tr align="center">
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>  
      </tr>
      <tr align="center">
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
      </tr>
      <tr align="center">
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
        <td bgcolor="#F0F8FF" width="80px"></td>
      </tr>
    </table>
	<script src = "/js/15.js"></script>
  </body>
</html>


function ChangeTextLine()
{
  var a1 = Math.floor(Math.random()*15 + 1);
  var a2 = Math.floor(Math.random()*15 + 1);
  var a3 = Math.floor(Math.random()*15 + 1);
  var a4 = Math.floor(Math.random()*15 + 1);
  var a5 = Math.floor(Math.random()*15 + 1);
  var a6 = Math.floor(Math.random()*15 + 1);
  var a7 = Math.floor(Math.random()*15 + 1);
  var a8 = Math.floor(Math.random()*15 + 1);
  var a9 = Math.floor(Math.random()*15 + 1);
  var a10 = Math.floor(Math.random()*15 + 1);
  var a11 = Math.floor(Math.random()*15 + 1);
  var a12 = Math.floor(Math.random()*15 + 1);
  var a13 = Math.floor(Math.random()*15 + 1);
  var a14 = Math.floor(Math.random()*15 + 1);
  var a15 = Math.floor(Math.random()*15 + 1);
  
  document.getElementById("td1").write(a1);
}

window.onload = ChangeTextLine;

ruslan_mart 16.06.2015 19:09

document.getElementById("td1").innerHTML = a1;

Дмитрий_Кирсанов 16.06.2015 20:55

Подскажите, пожалуйста, что будет означать запись:
window['a1'] в файле с кодом javascript;
Это обращение к переменной или обращение к html элементу с идентификатом а1? И почему не работает alert(window['a1'])?

Дмитрий_Кирсанов 16.06.2015 20:56

function ChangeTextLine()
{
  var a1 = Math.floor(Math.random()*15 + 1);
  var a2 = Math.floor(Math.random()*15 + 1);
  var a3 = Math.floor(Math.random()*15 + 1);
  var a4 = Math.floor(Math.random()*15 + 1);
  var a5 = Math.floor(Math.random()*15 + 1);
  var a6 = Math.floor(Math.random()*15 + 1);
  var a7 = Math.floor(Math.random()*15 + 1);
  var a8 = Math.floor(Math.random()*15 + 1);
  var a9 = Math.floor(Math.random()*15 + 1);
  var a10 = Math.floor(Math.random()*15 + 1);
  var a11 = Math.floor(Math.random()*15 + 1);
  var a12 = Math.floor(Math.random()*15 + 1);
  var a13 = Math.floor(Math.random()*15 + 1);
  var a14 = Math.floor(Math.random()*15 + 1);
  var a15 = Math.floor(Math.random()*15 + 1);
  
  document.getElementById("td1").innerHTML = a1;
  alert(window['a1']);
}

window.onload = ChangeTextLine;

tsigel 17.06.2015 08:52

Дмитрий_Кирсанов,
Все глобальные переменные лежат в объекте window. Вы задаёте вопросы по самым основам, которые надо просто почитать. Ищите область видимости, обращение к свойствам объекта, массива, читайте про объекты и массивы и все ваши вопросы отпадут.

Дмитрий_Кирсанов 17.06.2015 10:51

tsigel, подскажите, пожалуйста, как мне вывести значение свойства объекта a1 в ячейку html таблицы не инлайновым методом?

window["a1"] = a1;

<table>
<tr>
<td id = "sq1"></td>
<td id = "sq2"></td>
<td id = "sq3"></td>
<td id = "sq4"></td>
</tr>


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