【问题标题】:Javascript function embedded in HTML that is embedded in PHP not working properly?嵌入在 PHP 中的 HTML 中嵌入的 Javascript 函数无法正常工作?
【发布时间】:2014-04-19 18:27:36
【问题描述】:

这个标题好难听…… 嘿人们!我在一个学校的网络应用程序项目中遇到了这个错误,在过去一周左右尝试了替代方法后我无法通过。我希望有人能够捕捉到我无法捕捉到的东西......

好的,这是我认为是问题根源的代码:

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

这里是 Javascript 函数:

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

(这是名为“BattleMethods.js”的文件中的唯一代码;开始正文标记上方的脚本标记表明我们将使用此文件。如果有帮助,我也可以复制该行...) 尚未传递任何效果(即对敌人健康的影响)。我想先让它运行,然后再编写代码。

现在有了给定的信息,我需要进行以下操作:

1) 玩家点击右侧边栏 div 中的 Attack 按钮

2) 显示敌人的战斗屏幕div下方的TextLog div显示当前相关信息(即:“你攻击敌人!”)

3) 客户端变量适当更新(userHP、currentTurn 等)

就是这样。如果我能弄清楚当玩家单击“攻击”按钮时不允许函数做出反应的原因,我可以毫无问题地更新变量。我在物品商店做过类似的事情,效果很好。所以我真的很茫然。我希望这是足够的信息!任何帮助深表感谢。

脚本所在的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";

【问题讨论】:

  • "开始正文标签上方的脚本标签" - 你的意思是在它里面,还是在&lt;head&gt; 里面?虽然没关系,但将它们作为 &lt;html&gt; 的子级是无效的。
  • $currentTurn 的值是多少?请发布 PHP 生成的 HTML。
  • if($enemySPD
  • 好的,看起来不错(是的,您确实应该删除该评论并仅将其编辑到问题中)

标签: javascript php html function button


【解决方案1】:

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


你打错了,
document.getElementById.("TextLog").innerHTML = "You attack the enemy!";
应该是的

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2020-05-06
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    相关资源
    最近更新 更多