整个页面都贴上了,可能IE会不兼容,不过我只要在FF下没问题就可以了,来说说主要问题吧,我的初衷是页面上的div,单击一下,可以获得这个DIV的id,关键是这个DIV是要用insertHtml()这函数动态插进去的,这函数主要是解决非IE下insertAdjacentHTML不能用的问题,现在问题是,不是用这函数插进去的div可以获得id,但是用这函数插进去的div就不能获得id,有点弄不大明白,恳请各位多多指教,先谢过各位了
<!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>
<style type="text/css">
.tablebox{
position:absolute;
border-bottom:dashed 1px;
width:398px;
height:30px;
line-height:30px;
z-index:999;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs
/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".tablebox").click(function()
{
var id = $(this).attr("id");
alert(id);
});
});
function qwe(){
insertHtml("beforeEnd",document.getElementById("tablebox"),"<a href='#'><div class='tablebox' name='tablebox' id='1'> 23423453452345</div></a>");
}
function insertHtml(where, el, html){
where = where.toLowerCase();
if(el.insertAdjacentHTML){
switch(where){
case "beforebegin":
el.insertAdjacentHTML('BeforeBegin', html);
return el.previousSibling;
case "afterbegin":
el.insertAdjacentHTML('AfterBegin', html);
return el.firstChild;
case "beforeend":
el.insertAdjacentHTML('BeforeEnd', html);
return el.lastChild;
case "afterend":
el.insertAdjacentHTML('AfterEnd', html);
return el.nextSibling;
}
throw 'Illegal insertion point -> "' + where + '"';
} else {
var range = el.ownerDocument.createRange();
var frag;
switch(where){
case "beforebegin":
range.setStartBefore(el);
frag = range.createContextualFragment(html);
el.parentNode.insertBefore(frag, el);
return el.previousSibling;
case "afterbegin":
if(el.firstChild){
range.setStartBefore(el.firstChild);
frag = range.createContextualFragment(html);
el.insertBefore(frag, el.firstChild);
return el.firstChild;
}else{
el.innerHTML = html;
return el.firstChild;
}
case "beforeend":
if(el.lastChild){
range.setStartAfter(el.lastChild);
frag = range.createContextualFragment(html);
el.appendChild(frag);
return el.lastChild;
}else{
el.innerHTML = html;
return el.lastChild;
}
case "afterend":
range.setStartAfter(el);
frag = range.createContextualFragment(html);
el.parentNode.insertBefore(frag, el.nextSibling);
return el.nextSibling;
}
throw 'Illegal insertion point -> "' + where + '"';
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<input type="button" onclick="qwe()" />
<div id="mainbox" style="position:absolute">
<div id="listbox" style="border:dashed 1px; width:400px; height:443px; position:absolute;">
<div id="top" style="border-bottom:dashed 1px; width:400px; height:35px;"></div>
<div id="tablebox" style="overflow-x:hidden; width:400px; height:407px;">
<a href="#"><div id="1" class="tablebox">24234</div></a>
<a href="#"><div id="1" class="tablebox">24234</div></a>
<a href="#"><div id="1" class="tablebox">24234</div></a>
</div>
</div>
</div>
</body>
</html>
<!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>
<style type="text/css">
.tablebox{
position:absolute;
border-bottom:dashed 1px;
width:398px;
height:30px;
line-height:30px;
z-index:999;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs
/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".tablebox").click(function()
{
var id = $(this).attr("id");
alert(id);
});
});
function qwe(){
insertHtml("beforeEnd",document.getElementById("tablebox"),"<a href='#'><div class='tablebox' name='tablebox' id='1'> 23423453452345</div></a>");
}
function insertHtml(where, el, html){
where = where.toLowerCase();
if(el.insertAdjacentHTML){
switch(where){
case "beforebegin":
el.insertAdjacentHTML('BeforeBegin', html);
return el.previousSibling;
case "afterbegin":
el.insertAdjacentHTML('AfterBegin', html);
return el.firstChild;
case "beforeend":
el.insertAdjacentHTML('BeforeEnd', html);
return el.lastChild;
case "afterend":
el.insertAdjacentHTML('AfterEnd', html);
return el.nextSibling;
}
throw 'Illegal insertion point -> "' + where + '"';
} else {
var range = el.ownerDocument.createRange();
var frag;
switch(where){
case "beforebegin":
range.setStartBefore(el);
frag = range.createContextualFragment(html);
el.parentNode.insertBefore(frag, el);
return el.previousSibling;
case "afterbegin":
if(el.firstChild){
range.setStartBefore(el.firstChild);
frag = range.createContextualFragment(html);
el.insertBefore(frag, el.firstChild);
return el.firstChild;
}else{
el.innerHTML = html;
return el.firstChild;
}
case "beforeend":
if(el.lastChild){
range.setStartAfter(el.lastChild);
frag = range.createContextualFragment(html);
el.appendChild(frag);
return el.lastChild;
}else{
el.innerHTML = html;
return el.lastChild;
}
case "afterend":
range.setStartAfter(el);
frag = range.createContextualFragment(html);
el.parentNode.insertBefore(frag, el.nextSibling);
return el.nextSibling;
}
throw 'Illegal insertion point -> "' + where + '"';
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<input type="button" onclick="qwe()" />
<div id="mainbox" style="position:absolute">
<div id="listbox" style="border:dashed 1px; width:400px; height:443px; position:absolute;">
<div id="top" style="border-bottom:dashed 1px; width:400px; height:35px;"></div>
<div id="tablebox" style="overflow-x:hidden; width:400px; height:407px;">
<a href="#"><div id="1" class="tablebox">24234</div></a>
<a href="#"><div id="1" class="tablebox">24234</div></a>
<a href="#"><div id="1" class="tablebox">24234</div></a>
</div>
</div>
</div>
</body>
</html>
var mySpan=document.createElement('span');
document.getElementById(node).appendChild(mySpan)