JavaScript“onClick”事件
我的 PHP 文件中有这一行:
echo "<input type='button' id='showButton' value='show' onclick='showOld()'>";
显示了按钮,但单击它时没有任何反应。
该函数位于外部 JS 文件中。 这是函数声明:
页面中的其他 onclick
事件和 JS 函数正在运行。
function showOld()
{
alert("njkh");
var table = document.getElementById("showExp");
var button = document.getElementById("showButton");
if (table.style.display == "none")
{
table.style.display = "inline";
button.value = "הסתר ניסויים ישנים";
}
else if (table.style.display == "inline")
{
table.style.display = "none";
button.value = "הצג את כל הניסויים";
}
}
控制台说:
“ReferenceError:showOld 未定义”
I have this line in my PHP file:
echo "<input type='button' id='showButton' value='show' onclick='showOld()'>";
The button is displayed but when it is clicked, nothing happens.
The function is located in an external JS file.
this is function declaration:
Other onclick
events and JS functions in the page are working.
function showOld()
{
alert("njkh");
var table = document.getElementById("showExp");
var button = document.getElementById("showButton");
if (table.style.display == "none")
{
table.style.display = "inline";
button.value = "הסתר ניסויים ישנים";
}
else if (table.style.display == "inline")
{
table.style.display = "none";
button.value = "הצג את כל הניסויים";
}
}
console says:
"ReferenceError: showOld is not defined"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须确保更新的代码已部署。
你可以检查html源。
并且您发布的代码没有任何问题。
you must make sure your updated code had been deployed.
you can check the html source .
and your posted code has no any problem.
尝试打开 firebug 的控制台(或 chrome 开发工具)并检查是否有任何脚本错误。
您应该避免编写内联 JavaScript。它使调试和维护代码库变得更加困难。我建议您阅读以下有关 javascript 事件的文章:
Quirksmode - 早期事件处理程序
Quirksmode - 传统事件注册模型
文章引用:
Try opening up firebug's console (or chrome dev tools) and check for any script errors.
You should avoid writing inline javascript. It makes it harder to debug and maintain your codebase. I recommend that you read the following articles on javascript events:
Quirksmode - Early Event Handlers
Quirksmode - Traditional event registration model
A quote from the article:
首先,简化问题,看看下面的函数是否有效。
First, simplify the problem, see if the following function works.
接下来尝试检查异常:
javascript 控制台中到底显示了什么(如果可用)?
Try next to check for exceptions:
And what exactly is displayed in the javascript console (if available)?