Noa*_*Gal 2 javascript onclick
我试图从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..!");
}
Run Code Online (Sandbox Code Playgroud)
任何帮助?
ps没有错误.
当你写作
document.getElementById('one').onclick=alert("A..!");
Run Code Online (Sandbox Code Playgroud)
你设置为onclick处理程序的返回值alert("A..!"):它的undefined.所以这不行.
你需要的是一个函数定义:
document.getElementById('one').onclick = function() {alert("A..!");};
Run Code Online (Sandbox Code Playgroud)
或者,您可以:
function myfunc() {
alert("A..!");
}
document.getElementById('one').onclick = myfunc;
Run Code Online (Sandbox Code Playgroud)
但是编写匿名函数定义很好,因为它将代码保存在使用它的位置,因此通常更清晰.
你还需要在脚本元素中使用javascript:
<script>
document.getElementById('one').onclick = function() {alert("A..!");};
document.getElementById('two').onclick = function() {alert("B..!");};
document.getElementById('three').onclick = function() {alert("C..!");};
</script>
Run Code Online (Sandbox Code Playgroud)
以下是您网页的完整,经过修复和测试的完整版本:
<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>??????????
Run Code Online (Sandbox Code Playgroud)
(我还修复了HTML注释和缺失的tr)
您可以在此处单击测试:单击"单击此处"以激活单击其他三个部分(一,二,三)的功能.
| 归档时间: |
|
| 查看次数: |
23276 次 |
| 最近记录: |