2015-10-03 3 views
1

У меня есть <td> с идентификатором <td id="firstApp">. Я пишу событие onclick для кнопки. при нажатии кнопки showDate функция iscalled. Я хочу передать «id» в качестве параметра этой функции showDate, чтобы моя функция узнала, на каком id она должна действовать.JavaScript, передавая id как параметр в функцию

Я попытался,

<td><button type="button" onclick='document.getElementById("firstApp").innerHTML=Date()'> 

Затем он работает отлично.

Но когда я прохожу идентификатор, как вызов функции,

<td><button type="button" onclick='showDate("firstApp")'> 

Тогда это не будет работать, кто-нибудь может сказать мне, где я пошло не так ... !!

<!DOCTYPE html> 
<html> 
    <head> 
    </head> 
    <body> 
     <tr> 
      <td>App 1</td> 
      <td><button type="button" onclick='showDate("firstApp")' name="app1" >on/off</td> 
      <td>10</td> 
      <td id="firstApp"></td> 
      <td></td> 
     </tr> 
     <!-- <button type="button">Update</button> <button type="button">Log Out</button> --> 
     </table> 
     </div> 
     <script> 
      //write script herei 
      function showDate(appId) 
      { 

       document.getElementById("appId").innerHTML = Date(); 
      } 
     </script> 
    </body> 
</html> 

ответ

1

Вы не хотите, чтобы поставить кавычки вокруг аргумента при его использовании:

function showDate(appId) { 
    document.getElementById(appId).innerHTML = Date(); 
    // No quotes here ------^----^ 
} 
1
<!DOCTYPE html> 
<html> 
    <head> 
    </head> 
    <body> 
     <div> 
     <table> 
     <tr> 
      <td>App 1</td> 
      <td><button type="button" onclick='showDate("firstApp")' name="app1" >on/off</td> 
      <td>10</td> 
      <td id="firstApp"></td> 
      <td></td> 
     </tr> 
     <!-- <button type="button">Update</button> <button type="button">Log Out</button> --> 
     </table> 
     </div> 
     <script> 
      //write script herei 
      function showDate(appId) 
      { 

       document.getElementById(appId).innerHTML = Date(); 
      } 
     </script> 
    </body> 
</html> 

Удалить цитата из APPID

document.getElementById(appId).innerHTML = Date(); 
Смежные вопросы