使用javascript比较两个html按钮的onclick动作
我有这两个 HTML 表单按钮,每个按钮都有一个关联的 onclick 操作。
<input type=button name=sel value="Select all" onclick="alert('Error!');">
<input type=button name=desel value="Deselect all" onclick="alert('Error!');">
不幸的是,这个动作会不时发生变化。它可以是
onclick="";>
或者
onclick="alert('Error!');"
不幸的是,
onclick="checkAll('stato_nave');"
我正在尝试编写一些 javascript 代码来验证调用的函数是什么,并在需要时更改它:
var button=document.getElementsByName('sel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
if( button.getAttribute("onclick") != "checkAll('stato_nave');" &&
button.getAttribute("onclick") != ""){
//modify button
document.getElementsByName('sel')[0].setAttribute("onclick","set(1)");
document.getElementsByName('desel')[0].setAttribute("onclick","set(0)");
} //set(1) and set(0) being two irrelevant function
这些都不起作用。 回顾一些步骤,我注意到它
alert( document.getElementsByName('sel')[0].onclick);
没有像我预期的那样输出 onclick 内容,而是输出:
function onclick(event) {
alert("Error!");
}
所以我猜由于这个原因比较失败,我无法将 function
与 string 进行比较
。
有人猜测如何区分哪个函数与 onclick
属性关联吗?
I have this two HTML Form buttons with an onclick action associated to each one.
<input type=button name=sel value="Select all" onclick="alert('Error!');">
<input type=button name=desel value="Deselect all" onclick="alert('Error!');">
Unfortunately this action changes from time to time. It can be
onclick="";>
or
onclick="alert('Error!');"
or
onclick="checkAll('stato_nave');"
I'm trying to write some javascript code that verifies what is the function invoked and change it if needed:
var button=document.getElementsByName('sel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
if( button.getAttribute("onclick") != "checkAll('stato_nave');" &&
button.getAttribute("onclick") != ""){
//modify button
document.getElementsByName('sel')[0].setAttribute("onclick","set(1)");
document.getElementsByName('desel')[0].setAttribute("onclick","set(0)");
} //set(1) and set(0) being two irrelevant function
Unfortunately none of this work.
Going back some steps I noticed that
alert( document.getElementsByName('sel')[0].onclick);
does not output the onclick content, as I expected, but outputs:
function onclick(event) {
alert("Error!");
}
So i guess that the comparisons fails for this reason, I cannot compare a function
with a string
.
Does anyone has a guess on how to distinguish which function is associated to the onclick
attribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
onclick
属性转换为string
。那么你至少可以检查checkAll
的索引以及它是否为空。之后,您可以轻松地将这些input
元素绑定到新的onclick
函数。工作示例: http://jsfiddle.net/fAJ6v/1/
工作示例,当有 <代码>checkAll方法:http://jsfiddle.net/fAJ6v/3/
Try casting the
onclick
attribute to astring
. Then you can at least check the index ofcheckAll
and whether it is empty. After that you can bind thoseinput
elements to the newonclick
functions easily.working example: http://jsfiddle.net/fAJ6v/1/
working example when there is a
checkAll
method: http://jsfiddle.net/fAJ6v/3/这有效
http://jsfiddle.net/mplungjan/HzvEh/
但是为什么不将 onclick 移动到脚本
This works
http://jsfiddle.net/mplungjan/HzvEh/
But why not move the onclick to a script