Onclick函数,innerHTML和创建表

时间:2014-11-21 21:22:52

标签: javascript html

所以这里是代码:



window.onload=function(e){
//Created by Firestar001
	var X;
	var Y;
	
	var board="";
	
	var rizzalt=document.getElementById("rezzalt");
	
	var letters = new Array;
	letters = ["A","B","C","D","E","F","G","H","I","J"];
	
	for(a=0; a<=9; a++){
	board+="<tr>";
		for(b=1;b<=14; b++)
	{
		board+="<td id=/'"+ letters[a] +  b +"/' onClick=/'AreaCode()/'>" + letters[a]+b+"</td>";
	}
	board+="</tr>";
	}
	
	var Grade = document.getElementById('friend_water');
	Grade.innerHTML = board;
	}
	function AreaCode(){
	console.log("I work");
      rizzalt.innerHTML+="Works ";
	}
&#13;
<table id="friend_water" border="1 solid black">jklol</table><p></p><p id="rezzalt"></p>
&#13;
&#13;
&#13;

我已经包含了&#34; rizzalt rezzalt&#34;在那里使用stackoverflow上的代码snippit事件提供确认。

因此,它将创建一个战舰网格,A-J,1-14。我想要发生的是当一个玩家点击一个正方形(例如:C4)时,我希望控制台说出“#34;我工作&#34;所以......我知道它有效。该表正确加载但当我单击一个单元格时它给了我:Uncaught SyntaxError:Unexpected token ILLEGAL

引用html中第1行<!DOCTYPE html>行。

谢谢大家的帮助。

编辑:已解决。

再次感谢大家的帮助。代码正常工作,现在我可以开始更多的战舰游戏。

3 个答案:

答案 0 :(得分:1)

请检查您修改后的代码版本

var rizzalt=document.getElementById("rezzalt");
window.onload=function(e){
//Created by Firestar001
	var X;
	var Y;
	
	var board="";
	
	
	
	var letters = new Array;
	letters = ["A","B","C","D","E","F","G","H","I","J"];
	
	for(a=0; a<=9; a++){
	board+="<tr>";
		for(b=1;b<=14; b++)
	{
		board+="<td id='"+ letters[a] +  b +"' onClick='AreaCode()'>" + letters[a]+b+"</td>";
	}
	board+="</tr>";
	}
	
	var Grade = document.getElementById('friend_water');
	Grade.innerHTML = board;
	}
	function AreaCode(){
	console.log("I work");
      rezzalt.innerHTML+="Works ";
	}
<table id="friend_water" border="1 solid black">jklol</table><p></p><p id="rezzalt"></p>

答案 1 :(得分:0)

你的逃脱角色是错误的。它应该是\,而不是/

&#13;
&#13;
window.onload=function(e){
//Created by Firestar001
	var X;
	var Y;
	
	var board="";
	
	var rizzalt=document.getElementById("rezzalt");
	
	var letters = new Array;
	letters = ["A","B","C","D","E","F","G","H","I","J"];
	
	for(a=0; a<=9; a++){
	board+="<tr>";
		for(b=1;b<=14; b++)
	{
       //error was below.  all escape slashes were the wrong slash.
		board+="<td id=\'"+ letters[a] +  b +"\' onClick=\'AreaCode()\'>" + letters[a]+b+"</td>";
	}
	board+="</tr>";
	}
	
	var Grade = document.getElementById('friend_water');
	Grade.innerHTML = board;
	}
	function AreaCode(){
	console.log("I work");
      rizzalt.innerHTML+="Works ";
	}
&#13;
<table id="friend_water" border="1 solid black">jklol</table><p></p><p id="rezzalt"></p>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试:

board+='<td id="' + letters[a] +  b + '" onClick="AreaCode()">' + letters[a]+b+'</td>';