js如何获取动态生成的id 100
<c:choose><c:whentest="${fn:length(tablist)>0}"><divid="tabs"class="editarea"><ulid="...
<c:choose>
<c:when test="${fn:length(tablist) > 0}">
<div id="tabs" class="editarea">
<ul id="tabsAll">
<c:forEach items="${tablist}" var="tablist">
<c:choose>
<c:otherwise>
<=----------------------a${tablist['tabid']}----------------------------------------->
<li id="li${tablist['tabid']}">
<a id="a${tablist['tabid']}" href="#${tablist['tabid']}">${tablist['tabname']}</a>
</li>
</c:otherwise>
</c:choose>
</c:forEach>
</ul>
<c:forEach items="${tablist}" var="tablist" varStatus="vars">
<c:choose>
<c:otherwise><------div id-------------<div id="${tablist['tabid']}">${tablist['tabname']}</div>----------------------------->
<div id="${tablist['tabid']}">${tablist['tabname']}</div>
</c:otherwise>
</c:choose>
</c:forEach>
</div>
</c:when>
---------------------------------------------------》在线等
<li id="li${tablist['tabid']}">的id和div中的id获得
求js代码 展开
<c:when test="${fn:length(tablist) > 0}">
<div id="tabs" class="editarea">
<ul id="tabsAll">
<c:forEach items="${tablist}" var="tablist">
<c:choose>
<c:otherwise>
<=----------------------a${tablist['tabid']}----------------------------------------->
<li id="li${tablist['tabid']}">
<a id="a${tablist['tabid']}" href="#${tablist['tabid']}">${tablist['tabname']}</a>
</li>
</c:otherwise>
</c:choose>
</c:forEach>
</ul>
<c:forEach items="${tablist}" var="tablist" varStatus="vars">
<c:choose>
<c:otherwise><------div id-------------<div id="${tablist['tabid']}">${tablist['tabname']}</div>----------------------------->
<div id="${tablist['tabid']}">${tablist['tabname']}</div>
</c:otherwise>
</c:choose>
</c:forEach>
</div>
</c:when>
---------------------------------------------------》在线等
<li id="li${tablist['tabid']}">的id和div中的id获得
求js代码 展开
3个回答
展开全部
通过jquery获取ID,然后拆分ID,就可以获取你要的id了
<div id="getid">
<ul>
<li id="li1">标题1</li>
<li id="li2">标题2</li>
<li id="li3">标题3</li>
</ul>
</div>
<script type="text/javascript">
$("#getid li").each(function(){
var li_id = $(this).attr("id");
id = li_id.replace("li","");
});
</script>
<div id="getid">
<ul>
<li id="li1">标题1</li>
<li id="li2">标题2</li>
<li id="li3">标题3</li>
</ul>
</div>
<script type="text/javascript">
$("#getid li").each(function(){
var li_id = $(this).attr("id");
id = li_id.replace("li","");
});
</script>
展开全部
.你的问题的解决原理是在你那些Li和div的父对象绑定事件(你需要在什么事件获得li和div的id,就绑定什么事件),由于你没有给出父对象是什么,所以我就直接绑定在body的click下了
var body = document.getElementsByTagName('BODY')[0];
body.onclick = function(e){
e = e.target || event.srcElement;
//body下所有div和li有id的都会alert出来,所以是需要把事件绑定到你的div和li的父对象,这就绝对没有误差了
if(e.getAttribute('id') && (e.tagName == 'DIV' || e.tagName == 'LI')) alert(e.getAttribute('id'));
}
var body = document.getElementsByTagName('BODY')[0];
body.onclick = function(e){
e = e.target || event.srcElement;
//body下所有div和li有id的都会alert出来,所以是需要把事件绑定到你的div和li的父对象,这就绝对没有误差了
if(e.getAttribute('id') && (e.tagName == 'DIV' || e.tagName == 'LI')) alert(e.getAttribute('id'));
}
追问
我现在把具体的内容已经修改了,请您详细给我写一下啊,麻烦您了!
追答
var body = document.getElementById('tabs');
body.onclick = function(e){
e = e.target || event.srcElement;
if(e.getAttribute('id') && (e.tagName == 'DIV' || e.tagName == 'LI')) alert(e.getAttribute('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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function clicks(v)
{
alert(v);
}
</script>
</head>
<body>
<button type="button" id="b1" onclick="clicks(this.id);">
Click me
</button>
<button type="button" id="b2" onclick="clicks(this.id);">
Click me
</button>
<button type="button" id="b3" onclick="clicks(this.id);">
Click me
</button>
<button type="button" id="b4" onclick="clicks(this.id);">
Click me
</button>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function clicks(v)
{
alert(v);
}
</script>
</head>
<body>
<button type="button" id="b1" onclick="clicks(this.id);">
Click me
</button>
<button type="button" id="b2" onclick="clicks(this.id);">
Click me
</button>
<button type="button" id="b3" onclick="clicks(this.id);">
Click me
</button>
<button type="button" id="b4" onclick="clicks(this.id);">
Click me
</button>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询