我使用 PHP 输出信息行。我试图为每一行添加一个隐藏/显示。我“ID”该行,然后添加一个简单的文本“隐藏/显示”按钮,该按钮引用 ID 行并尝试切换“显示”属性。
我试过转义引号,将它们更改为单引号,添加/删除括号。我不知所措。
echo "<table width=\"640\" style=\"margin-left: auto; margin-right: auto;\"><tr id=\"JEntry".$data['ID']."\" style=\"display: visible\"><td><button onmousedown=\"javascript: if (document.getElementById(\"JEntry".$data['ID']."\").style.display != \"visible\") { document.getElementById(\"JEntry".$data['ID']."\").style.display = \"visible\"; } else { document.getElementById(\"JEntry".$data['ID']."\").style.display = \"none\"; }\">Hide or Show</button>";
意外的标记 ”}”
您的 PHP 代码很好。但是,生成的 HTML 需要对其属性进行转义。
例如,您的代码输出类似
<button onmousedown="javascript: if (document.getElementById("JEntry")) {}"></button>
正如您希望看到的,javascript 中的引号与属性值的引号匹配。这将导致解析问题。
要解决此问题,请仅在 javascript 中使用单引号
<button onmousedown="javascript: if (document.getElementById('JEntry')) {}"></button>
或逃离他们。
<button onmousedown="javascript: if (document.getElementById(\"JEntry\")) {}"></button>
在我看来,第一个选项看起来更整洁,尤其是当您考虑需要转义反斜杠 ( \
) 时。所以你的固定代码是
echo "<table width=\"640\" style=\"margin-left: auto; margin-right: auto;\"><tr id=\"JEntry".$data['ID']."\" style=\"display: visible\"><td><button onmousedown=\"javascript: if (document.getElementById('JEntry".$data['ID']."').style.display != 'visible') { document.getElementById('JEntry".$data['ID']."').style.display = 'visible'; } else { document.getElementById('JEntry".$data['ID']."').style.display = 'none'; }\">Hide or Show</button>";
阅读此内容的方式是未转义双引号 ( "
) 是 PHP 的一部分,转义双引号 ( \"
) 是 HTML 的一部分,单引号 ( '
) 是 JS 的一部分。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句