简体   繁体   English

从javascript更改td的“ onclick”值

[英]changing the value of “onclick” of a td from javascript

I am trying to change the value of "onclick" of a td from a function in javascript. 我试图从javascript中的函数更改td的“ onclick”值。 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. 我已经尝试了所有可以想到的东西,并且在互联网上进行了搜索,但是没有任何效果,下面的代码提供了这一点,但是它的问题是它正在执行我想要的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? 任何帮助?

ps there are no errors. ps没有错误。

When you write 当你写作

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

you set as onclick handler the the returned value of alert("A..!") : it's undefined . 您将alert("A..!")的返回值设置为onclick处理程序: undefined So this can't work. 所以这行不通。

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). 您可以在此处单击测试:单击“单击此处”以激活单击其他三个部分(一个,两个,三个)的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM