javascript函数奇怪问题???在线等!!
下面的html页面是可以正常运行的,但是
将 var jadminid= "88888 "; 改为其他例如 var jadminid= "aaa "后;
在点 "查看信息 "时就报错了, 不知道为什么???
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> javascript </TITLE>
<script type= "text/javascript ">
function send()
{
var jarea= "北京 ";
var jname= "beijng ";
var jadminid= "88888 ";
var row = document.createElement( "tr ");
var cell = document.createElement( "td ");
cell.innerHTML =jarea;
row.appendChild(cell);
cell = document.createElement( "td ");
cell.innerHTML =jname;
row.appendChild(cell);
cell = document.createElement( "td ");
cell.innerHTML= " <a href= '# ' onClick= 'getadmindetail( "+jadminid+ ") '> "+ "查看信息 "+ " </a> ";
row.appendChild(cell);
show_admin.appendChild(row);
}
function getadmindetail(jadminid)
{
alert(jadminid);
}
</script>
</HEAD>
<BODY>
<input type= "button " value= "生成表格 " onClick= "send(); "/>
<table border=1>
<tbody id= "show_admin ">
</tbody>
</table>
</BODY>
</HTML>
------解决方案--------------------把
cell.innerHTML= " <a href= '# ' onClick= 'getadmindetail( "+jadminid+ ") '> "+ "查看信息 "+ " </a> ";
改成
cell.innerHTML= " <a href= '# ' onClick= 'getadmindetail(\ " "+jadminid+ "\ ") '> 查看信息 </a> ";
由于没有引号,生成的页面代码就成了
<a href= '# ' onClick= 'getadmindetail(aaa) '> 查看信息 </a>
这把aaa当成变量了,当然有问题,转一下就个引号就成了
------解决方案--------------------var jadminid= "88888 "时,网页将jadminid作为数字处理
var jadminid= "aaa "时,网页将jadminid作为字符串处理
如果想让jadminid为字符串的话,可以这样写
cell.innerHTML= " <a href= '# ' onClick= 'getadmindetail( ' "+jadminid+ " ') '> "+ "查看信息 "+ " </a> ";
多加个引号应该就可以用了