2013-01-10 35 views
0

我想要做的是使Y [0]成爲圖像,而不是文字。現在它只是鏈接而不是圖片。使文字圖像

<html> 
<head> 
<script type="text/javascript"> 
function changeContent(){ 
    var x=document.getElementById('myTable').rows 
    var y=x[0].cells 
    y[0].innerHTML = "http://www.handicappedpets.com/wizards/img/wwmeasure2B.jpg" 
} 
</script> 
</head> 

<body> 
<table id="myTable" border="1"> 
<tr> 
<td>d</td> 
<td>d</td> 
</tr> 
<tr> 
<td>d</td> 
<td>d</td> 
</tr> 
</table> 
<form> 
<input type="button" onclick="changeContent()" value="Change content"> 
</form> 
</body> 

</html> 
+0

因此,使它的圖像元素,並將URL作爲'src'屬性? – Bergi

+0

你想解決什麼*問題*這種實現非常嚴格 - 有可能採取更好的方法。 – Sampson

回答

2

您只是將單元格的html設置爲恰好是url的文本。您需要在單元格內創建一個img標記:

y[0].innerHTML = '<img src="http://www.handicappedpets.com/wizards/img/wwmeasure2B.jpg" />' 
+0

就是這樣!謝謝:) – KDD

-1

您不能那樣做。你可以做的是將該URL放入標籤中並插入該標籤,或者創建一個新的Image()對象,設置它的src屬性並將其插入到DOM中。

+0

不,你不能把一個Javascript變量指向一個圖像。 Rhumborl建議將該網址放入標記(順便說一句,我建議),並且你已經低估了我的答案。謝謝。 –

0

嘗試這樣的:

y[0].innerHTML = '<img src="http://www.handicappedpets.com/wizards/img/wwmeasure2B.jpg" alt="Remove Criteria" />' 
0

什麼

y[0].innerHTML = '<img src="http://www.handicappedpets.com/wizards/img/wwmeasure2B.jpg">'; 
0

你需要保持圖像源的標籤,這樣

y[0].innerHTML = "<img src='http://www.handicappedpets.com/wizards/img/wwmeasure2B.jpg'/>" 
0

您可以創建一個使用javascript動態圖像並將其附加到單元格。

window.changeContent = function() { 
    var x = document.getElementById('myTable').rows, 
     cell = x[0].cells[0], 
     img = document.createElement('img'); 

    img.setAttribute('src', 'http://www.handicappedpets.com/wizards/img/wwmeasure2B.jpg'); 

    cell.innerHTML = ''; 
    cell.appendChild(img); 

} 

http://jsfiddle.net/C7KZu/