下面的代码是我使用 Python 构建的 Google App Engine 项目的一部分。我正在输入 x 和 y(此处未显示),并在此处将其打印为表格,其中 x,y 坐标作为表格中的文本。此外,我需要这样做,以便当您单击表格的单元格时,它会调用一个函数,该函数将返回一个字符串以供其弹出。为简单起见,此时它只是 X 和 Y。
所以简而言之,我需要一种将变量从 Python 传递到 HTML 的方法,以便可以将其传递给 javascript。我怎样才能做到这一点?
def testFunc(self, x, y):
return str(x) + " , " + str(y)
def drawTable(self, row , col):
write = self.response.write
write(row)
write(col)
write("<html><body><script> function tableInfo() { alert(\""+ self.testFunc(x, y)+""\");}</script><table>")
for y in range(row):
write("<tr>")
for x in range(col):
cell = "<td bgcolor=\"#00FF00\" onclick = \"tableInfo(x, y)\">" + str(x) + " " + str(y) + "</td>"
if x % 2 == 0:
cell = "<td bgcolor=\"#FF0000\" onclick = \"tableInfo(x, y)\">" + str(x) + " " + str(y) + "</td>"
write(cell)
write("</tr>")
write("</table></body></html>")