mycurrent_cell.onclick = new Function ( " return ShowFolderInfo(this) " ) ;
或者mycurrent_cell.onclick = function() { return ShowFolderInfo(this); }
或者mycurrent_cell.onclick = function() { return ShowFolderInfo(this); }
先用onmouseover试试
父窗口(f.htm)
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>父窗口</title>
</head>
<body><input type="button" value="添加表格" id="btn1" />
<table cellpadding="0" cellspacing="0" border="1" width="800">
<tr><td>下面开始增加表格</td>
</tr>
<tr><td id="tdcontent"></td></tr>
</table></body>
</html>
<script language="javascript">
var btn=document.all["btn1"];
btn.onclick=Function ("Add()");
function Add()
{
var left = (screen.width-280)/2 ;
var height = (screen.height-40)/2 - 40 ;
var param = "left="+left+",top="+height+",width=360,height=30,scrollbars=0,status=yes" ;
var openurl = "z.htm";
window.open(openurl,"添加表格",param);
}</script>
子窗口(z.htm)
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<input type="button" value="在子窗体给父窗体添加表格和事件" onclick="Add()" />
</body>
</html>
<script language="javascript">
function Add()
{
var parentwindow = window.opener;
var td = parentwindow.document.getElementById("tdcontent");
var tableobj = td.firstChild; while (tableobj != null && tableobj.type == 'folder' )
{
tableobj = tableobj.nextSibling ;
}
// 创建一个<table>元素和一个<tbody>元素
mytable = parentwindow.document.createElement("table");
mytablebody = parentwindow.document.createElement("tbody");
mycurrent_row = parentwindow.document.createElement("tr");
mycurrent_cell = parentwindow.document.createElement("td");
//在子窗口给父窗口注册事件失败,写了个最简单的函数
mycurrent_cell.onclick = Function("window.alert('aaa')");
//下面的鼠标样示都能起作用
mycurrent_cell.style.cursor = "move" ;
mycurrent_cell.setAttribute("width", "28") ;
mycurrent_cell.setAttribute("align", "center") ;
mycurrent_cell.setAttribute("valign", "top") ; //创建一个checkbox
currentElement = parentwindow.document.createElement("input");
currentElement.setAttribute("name", "checkbox");
currentElement.setAttribute("type", "checkbox");
//增加元素到<td>
mycurrent_cell.appendChild(currentElement);
//增加<td>到<tr>
mycurrent_row.appendChild(mycurrent_cell);
mytablebody.appendChild(mycurrent_row);
//增加<tbody>到<table>
mytable.appendChild(mytablebody);
mytable.setAttribute("type", "folder");
mytable.setAttribute("width", "50%");
mytable.setAttribute("border", "1");
mytable.setAttribute("cellspacing", "5");
mytable.setAttribute("cellpadding", "5");
var dd = td.insertBefore(mytable,tableobj);
window.close();
}
</script>
保存到本地即可运行。
btn.onclick=Function ("Add()");
上面明显不对的,1:JS对大小写是敏感的,
btn.onclick=Function ("Add()");-->btn.onclick=function () { add();}//直接追加事件是不能有参数的.
btn.onclick=Function ("Add()");
没有任何错误,楼上的到底运行过程序没,在本窗口的事件绑定一个函数,没有错误,只是如果在子窗口写的话就不行了
mycurrent_cell.onclick = Function("window.alert('aaa')");
Function("window.alert('aaa')");定义在子窗体,当子窗体关闭后,它就不存在了,父窗体响应事件时,就找不到它了。我试验了一下,在父窗体定义一个函数:
function test(){
alert("aaaaa");
} mycurrent_cell.onclick = Function("window.alert('aaa')");改成
mycurrent_cell.onclick = opener.test;
问题就解决了