2012-11-24 204 views
0

我想通過按下按鈕來改變表格中單元格的內容,而不使用輸入文本框。 我應該用什麼來做到這一點? 謝謝!HTML Javascript表格單元格的內容

這裏是我的代碼:

`

var images = new Array(); 
var i=0; 

images[0] ="1.jpg"; 
images[1] ="2.jpg"; 
images[2] ="3.jpg"; 
images[3] ="4.jpg"; 

names = ["New York", "Nagasaki", "Nottingham", "Netherlands"] 

function next(){ 
    if(i>=images.length){ 
     i=0; 
     document.getElementById('names').value=names[i]; 
     document.getElementById('img').src=images[i]; 
     i++; 
    } 
    else{ 
     document.getElementById('img').src=images[i]; 
     document.getElementById('names').value=names[i]; 
     i++; 
    } 
} 
</script> 
</head> 
<body><center> 
<h3>LabExercise3</h3> 
<h2>By <i><b><u>Nurbek Zhapar</u></b></i></h2></center> 
<form> 
<h1>Around the World</h1><br/> 
<hr/> 
<br/> 
    <table align="center"> 
     <tr><td><input type="text" value="" id="names" readonly="readonly"/></td></tr> 
      <tr><td><p style="text-align:center;"> 
      <img src="1.jpg" alt="" id="img"/> 
      </p></td></tr></table> 

<table align="center"> 
    <tr> 
     <td><input type="button" value=" Next " onclick="next()" /></td> 
    </tr> 
</table>   
</form>` 

我想替換我這裏使用的別的東西來顯示國名,或顯示在其他方式國名輸入文本框。謝謝!

+0

歡迎來到Stack Overflow。爲了有效地幫助你,我們需要更多的信息。你的表格是什麼樣的(發佈你的標記)?按鈕在哪裏?內容應該改成什麼? –

+0

謝謝理解!) – user1849636

+0

「不使用文本框」是什麼意思?只需按下按鈕,您發佈的內容就可以正常工作。 – pimvdb

回答

0

您可以在表格單元格內添加<span id="name"></span>,並更新document.getElementById("name").textContent而不是文本框。

你也應該考慮一下你的HTML和JavaScript。您正在使用的相當多的東西已被棄用。

請參閱http://jsfiddle.net/3sEYq/

+0

使用span爲我工作得很好,雖然在我的情況下textContent不合作。我不得不使用innerText。關於這個問題的博客文章很棒:[perfectionkills.com](http://perfectionkills.com/the-poor-misunderstood-innerText/) – Cfold

相關問題