HTML代码
<table border="0px" id="map-flags" align="center">
<tr><td><img src="flags/ug.jpeg" alt="Welcome" width="160px" height="160px"></td></tr>
</table>
Javascript代码
<script>
document.getElementbyId('tuts').addEventListener('click', function(e){
console.log(e.toElement.alt);
});
</script>
我试图在控制台中打印出ID,但它会带来此错误,可能是什么问题?
答案 0 :(得分:2)
添加ID
<table border="0px" id="map-flags" align="center">
<tr>
<td>
<img id="tuts" src="flags/ug.jpeg" alt="Welcome" width="160px" height="160px">
</td>
</tr>
</table>
并将getElementbyId更改为getElement ** B ** yId
<script>
document.getElementById('tuts').addEventListener('click', function(){
console.log(this.alt);
});
</script>
**编辑:更改console.log(e.toElement.alt);到console.log(this.alt); **