简体   繁体   English

PHP中嵌入的HTML中嵌入的Javascript函数无法正常工作?

[英]Javascript function embedded in HTML that is embedded in PHP not working properly?

That title is a mouthful... Hey people! 这个头衔实在令人...目结舌... I've been having this bug with a web app project for school that I haven't been able to get passed after trying alternate methods for the past week or so. 我在学校的一个Web应用程序项目中遇到了这个错误,在过去一周左右的时间里尝试其他方法后,我一直未能通过。 I'm hoping someone might be able to catch something that I can't... 我希望有人能够捕捉到我无法捕捉到的东西...

Okay so here's the code which I believe is the source of the problem: 好的,这是我认为是问题根源的代码:

 echo"<div class='RightSidebar'> <button id='AtkBtn' type='button' onclick='attack(".$currentTurn.");'><b>Attack</b></button></a>\n";

Here's the Javascript function: 这是Javascript函数:

function attack(turn)
{
    if(turn == 1 )
        {alert('It is not your turn!!');}
    else
    {
        document.getElementById.("TextLog").innerHTML = "You attack the enemy!";
    }
}

(This is the only code in a file named "BattleMethods.js"; the script tags above the opening body tag states we'll be using this file. I can copy that line too if it helps...) No effects are being passed yet (ie, the effect on the enemy's health). (这是名为“ BattleMethods.js”的文件中的唯一代码;开头body标记上方的script标记指示我们将使用此文件。如果有帮助,我也可以复制该行...)没有任何影响。过去了(即对敌人健康的影响)。 I want to get this running first and then code that out. 我想先运行它,然后将其编码。

Now with that given information I need the following to happen: 现在,有了给定的信息,我需要进行以下操作:

1) Player hits the Attack button within a right sidebar div 1)玩家点击右侧栏div中的“攻击”按钮

2) TextLog div below the battle screen div that shows the enemy reveals current relevant information (ie: "You attack the enemy!") 2)在显示敌人的战斗屏幕div下方的TextLog div会显示当前的相关信息(即:“您在攻击敌人!”)

3) Client-side variables update appropriately (userHP, currentTurn, etc) 3)客户端变量会适当更新(userHP,currentTurn等)

That is all. 就这些。 I can get the variables to update with no problem if I can figure out whats not allowing the function to react when player clicks the Attack button. 如果我能找出导致玩家单击“攻击”按钮时函数无法响应的内容,则可以毫无问题地更新变量。 I had done something similar in the item store and it worked fine. 我在商品商店做过类似的事情,而且效果很好。 So I'm really at a loss. 所以我真的很茫然。 I hope this is enough information! 我希望这是足够的信息! Any help is much appreciated. 任何帮助深表感谢。

HTML where script lies: 脚本所在的HTML:

echo"<!DOCTYPE html>\n";
echo"<html>\n";
echo"<head>\n";
echo"<title>SlateKeeper -- BATTLEROOM</title>\n";
echo"<meta name='viewport' content='minimum-scale=0.98; maximum-scale=5;initial-            scale=0.98;user-scalable=no;width=1024'>\n";

echo"<link rel='stylesheet' type = 'text/css' href='CSS/battleTowersStyles.css'>\n";
echo"<script language= 'Javascript' type = 'text/javascript' src='Javascript/BattleMethods.js'></script>\n";

echo"<style>\n";

echo"                body {background-color: #3b4870}\n";
echo"</style>\n";

echo"</head>\n";

Please see the following changes I've made to this code.It is working properly. 请查看我对此代码所做的以下更改。它工作正常。
These are the javascript changes. 这些是javascript的更改。 function attack(turn) { if(turn == 1 ) { alert('It is not your turn!!'); } else { document.getElementById('TextLog').innerHTML = "You attack the enemy!"; } }
PHP CODE IS PHP代码是
$turn = 0; echo"<div class='RightSidebar'> <button id='AtkBtn' type='button' onclick='attack($turn)'><b>Attack</b></button></a>\\n";


You had made mistake in the line, 您在生产线上犯了错误,
document.getElementById.("TextLog").innerHTML = "You attack the enemy!";
It should have been 应该是

document.getElementById("TextLog").innerHTML = "You attack the enemy!";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM