JS 之中OnClick得不到响应!求教

tc_yf 2012-06-16 10:43:25
大家好!有个问题请教大家!
我用Js写了一个链接,有一个OnClick函数,但是函数没有响应,请问有谁知道是怎么回事么?
代码如下:

//函数
function chgTokHide(Tok_Count)
{
var idTokControl = "tokControl"+Tok_Count;
var idTokHide = "tokHide"+Tok_Count;
var oControl = document.getElementById(idTokControl);
var oHide = document.getElementById(idTokHide);
if(oHide.style.display=="block")
{
oHide.style.display = "none";
oControl.innerHTML = "[+]";
}
else
{
oHide.style.display = "block";
oControl.innerHTML = "[-]";
}
}

................................
...............................
Tok_Count = 1;
document.write('<a href="#" onclick="return chgTokHide(Tok_Count);" id=\'tokControl\'');
document.write(Tok_Count);
document.write('>');
document.write('[+]');
document.write('</a>');

谢谢大家了
我看调试工具提示是 我定义的函数 is not defined,我明明在开头定义了,这有什么要注意的地方么?
...全文
287 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tc_yf 2012-06-18
  • 打赏
  • 举报
回复
谢谢了,我确实是新手,很多地方不太明白,谢谢指导!
那一笑的凄凉 2012-06-18
  • 打赏
  • 举报
回复
还有你函数传参myFunction(tcount)你觉得这样能传过去?建议你先学学javascript 基本语法
//在这里单独添加也不行
function myFunction() {

alert("Hello World!");
}
还有你的这个函数不行上面我也说了你一个document.write把整个页面重写了,要想函数能执行也得把函数写进去,把你的给你改了下你试试
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
//在这里单独添加也不行
function myFunction() {

alert("Hello World!");
}
</script>
</head>
<body>
<script type="text/javascript">
window.onload = function () {
var tcount = 10000;
document.write("<script type=\"text/javascript\">function myFunction(i){ alert(i);}<\/script>");
document.write('<a href ="#" onclick="myFunction('+tcount+')"> Click Me');
document.write('</a>');
}
</script>

</body>
</html>
那一笑的凄凉 2012-06-18
  • 打赏
  • 举报
回复
楼主我怎么说你呢,你在onload里面写
document.write('<a href ="#" onclick="myFunction(tcount)"> Click Me');
document.write('</a>');
这2句代码你明白什么意思吗?
不明白你可以右键查看页面源代码你会发现页面上就剩这一句了
<a href ="#" onclick="myFunction(tcount)"> Click Me</a>
那一笑的凄凉 2012-06-16
  • 打赏
  • 举报
回复
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<script type="text/javascript">
document.write('<button onclick="myFunction()"> Click Me');
document.write('</button>');
</script>
<p>By clicking the button above, a function will be called. The function will alert a message.</p>
</body>
</html>
这样就可以弹出来了
tc_yf 2012-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

函数正常调用了啊 问题是 var oHide = document.getElementById(idTokHide);没这个对应的对象啊
[/Quote]
那个定义了。我写了一个简单的代码来表现这个问题


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<script type="text/javascript">
document.write('<button onclick="return myFunciton();"> Click Me');
document.write('</button>');
</script>
<p>By clicking the button above, a function will be called. The function will alert a message.</p>
</body>
</html>


这个运行的话点击没有用,也是现实函数未定义。求教啊,是不是不能用document.write来写啊?
似梦飞花 2012-06-16
  • 打赏
  • 举报
回复
函数正常调用了啊 问题是 var oHide = document.getElementById(idTokHide);没这个对应的对象啊
suano 2012-06-16
  • 打赏
  • 举报
回复
如果你把函数写在onload事件里,那它只会在onload的时候定义或者执行一次,再去ONCLICK肯定没有了。
tc_yf 2012-06-16
  • 打赏
  • 举报
回复
谢谢您的回答,我仔细看了一下代码,我举得例子不太合适,我举得例子只是因为函数名写错了,
我重新写了一个例子,我觉得是因为window.onload函数的原因,但是不知道怎么解决,代码如下:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
//在这里单独添加也不行
function myFunction(i)
{
alert("Hello World!");
}
</script>
</head>
<body>
<script type="text/javascript">
window.onload = function()
{
//我在这里单独添加过,也不行
function myFunction(i)
{
alert("Hello World!");
}
var tcount=1;
document.write('<a href ="#" onclick="myFunction(tcount)"> Click Me');
document.write('</a>');
}
</script>
<p>By clicking the button above, a function will be called. The function will alert a message.</p>
</body>
</html>


谢谢了!

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧