Javascript 从javascript更改td的“onclick”值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11292295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:19:26  来源:igfitidea点击:

changing the value of "onclick" of a td from javascript

javascriptonclick

提问by Noam Gal

I am trying to change the value of "onclick" of a td from a function in javascript. I've tried everything i can think of and i've searched it on the internet but nothing worked, exept from this code below, but the problem with it is that it's executing what i want the value of onclick to be instead of changing it.

我正在尝试从 javascript 中的函数更改 td 的“onclick”值。我已经尝试了我能想到的所有方法,并且已经在互联网上搜索过它,但没有任何效果,除了下面的这段代码,但它的问题在于它正在执行我想要的 onclick 值而不是更改它.

<div align="center" id="bodyC">
<table width="800" height="410">
<td class="bodyCC">
    <table id="MYtable" class="MYtable" border="0" width="100%" height="100%" cellpadding="50">
    <td id="one" class="one" onclick=""></td>
    <td id="two" class="two" onclick=""></td>
    <td id="three" class="three" onclick=""></td>
    </table>
</td>
</table>
</div>


function download()
{
<!-- this function is called when the "Download" button on the website's menu is pressed, and is soppused to change the "body" table so when the sells are clicked an alert will pop.-->
document.getElementById('one').onclick=alert("A..!");
document.getElementById('two').onclick=alert("B..!");
document.getElementById('three').onclick=alert("C..!");
}

any help?

有什么帮助吗?

p.s. there are no errors.

ps没有错误。

回答by Denys Séguret

When you write

当你写

document.getElementById('one').onclick=alert("A..!");

you set as onclickhandler the the returned value of alert("A..!"): it's undefined. So this can't work.

您设置为onclick处理程序的返回值alert("A..!"):它的undefined。所以这是行不通的。

What you need is a function definition :

你需要的是一个函数定义:

document.getElementById('one').onclick = function() {alert("A..!");};

Alternatively, you could have :

或者,你可以有:

function myfunc() {
    alert("A..!");
} 
document.getElementById('one').onclick = myfunc;

But it's fine to write anonymous function definitions, as it keeps the code in the place where it is used and thus is often cleaner.

但是编写匿名函数定义很好,因为它将代码保存在使用它的地方,因此通常更干净。

You also need to have you javascript inside a script element :

您还需要在脚本元素中使用 javascript:

<script>
    document.getElementById('one').onclick = function() {alert("A..!");};
    document.getElementById('two').onclick = function() {alert("B..!");};
    document.getElementById('three').onclick = function() {alert("C..!");};
</script>

Here is a complete, fixed, tested version of your page :

这是您页面的完整、固定、经过测试的版本:

<div align="center" id="bodyC">
<table width="800" height="100"><tr>
<td class="bodyCC">
    <table id="MYtable" class="MYtable" border="0" width="100%" cellpadding="50"><tr>
    <td id="one" class="one" onclick="">ONE</td>
    <td id="two" class="two" onclick="">TWO</td>
        <td id="three" class="three" onclick="">THREE</td></tr>
    </table>
    </td></tr>
</table>
    <span onclick="download();">CLICK HERE</span>
</div>
<script>
// this function is called when the "Download" button on the website's menu
// is pressed, and is soppused to change the "body" table so when the sells
// are clicked an alert will pop.
function download() {
   document.getElementById('one').onclick=function(){alert("A..!");};
   document.getElementById('two').onclick=function(){alert("B..!");};
   document.getElementById('three').onclick=function(){alert("C..!");};
} 
</script>??????????

(I fixed also the HTML comments, and the missing tr)

(我还修复了 HTML 注释和缺失的 tr)

You can click test it here: click on "CLICK HERE" to activate the ability to click on the three other sections (one, two, three).

您可以在此处单击测试:单击“单击此处”以激活单击其他三个部分(一、二、三)的能力。