<table>
<tr>
<td id="td1"><script>alert(document.getElementsByTagName("script")[0].parentElement.id)</script></td>
</tr>
<tr>
<td id="td2"><script>alert(document.getElementsByTagName("script")[1].parentElement.id)</script></td>
</tr>
<tr>
<td id="td3"><script>alert(document.getElementsByTagName("script")[2].parentElement.id)</script></td>
</tr>
</table>
不大懂你的意思,这样??
<tr>
<td id="td1"><script>alert(document.getElementsByTagName("script")[0].parentElement.id)</script></td>
</tr>
<tr>
<td id="td2"><script>alert(document.getElementsByTagName("script")[1].parentElement.id)</script></td>
</tr>
<tr>
<td id="td3"><script>alert(document.getElementsByTagName("script")[2].parentElement.id)</script></td>
</tr>
</table>
不大懂你的意思,这样??
有办法么...
{
var ele=aa.parentNode;
alert(ele.innerHTML);}
</script>
</head><table>
<tr>
<td><a onclick="aa(this)">这里获得该标签外面的TD元素</a></td>
</tr>
<tr>
<td><a onclick="aa(this)">这里获得该标签外面的TD元素</a></td>
</tr>
<tr>
<td><a onclick="aa(this)">这里获得该标签外面的TD元素</a></td>
</tr>
</table></body>
</html>怎么不用这样子做
ctrl+c
查看源文件
要在页面加载的时候获得到...
我的目的是不用JS循环
所以我要在JSP循环的时候写一段JS代码,这样可以少循环一次
当然也就不能BYID BYNAME BYTAGNAME 或者 ONCLICK触发了
改变width??height??bgColor??
没有目的性啊?
...
</script>
汗,才想起来不能用循环,那我这个也不行什么都不能用,用什么js啊@!,干脆页面写死得了
不过如果是JSP输出的话,你可以直接给前面的TD一个ID值,然后在后面的JS中写上ID值
<td id="<%写值%>"><script>alert("TD的值是<%写值%>");</script></td>
我以为JS很强大, 不知道以后的W3C会不会加入获得当前代码所在标签的功能...
我想不明白,为什么就不能用ID或者name一类的呢???
如果ID NAME TAGNAME 的话,那就还是多了一次循环了..
这个要加载的时候操作,所以不能触发事件
的每一行进行求和<script>
<script>
function $Id(sId){
return document.getElementById(sId);
}function changeContent(sContent){
var oTab=$Id('table1');
oTab.rows[oTab.rows.length-1].cells[0].innerHTML=sContent;
}
</script>
<table id='table1'>
<tr>
<td>
111<script>changeContent("aaa")</script>
</td>
</tr>
<tr>
<td>
222<script>changeContent("bbb")</script>
</td>
</tr>
<tr>
<td>
333<script>changeContent("ccc")</script>
</td>
</tr>
</table>
这样不错啊.我怎么没想到呢.,因为行是生成的,所以每次的最后一行就是我想要的了.
这样也少一次循环.
谢咯.
另外回29楼
因为是生成的表格.我不知道有多少行.当然得循环了.而不是去深究getElementById有没有循环不过我还是想知道.<script标签里的代码能不能找到父元素 - -
....
为什么不能获取到??
你就不能改一改??document.getElementsByTagName("script")[2].parentElement.style.width=xxx
document.getElementsByTagName("script")[1].parentElement.style.height=xxx
...
虽然可能和你的需求不一样,但能获取这是事实啊
所以我说....
这里可能有3行,也可能20行.
如果getElementsByTagName 我要改变这些值,我是不是必须用 for 呢
与我的初衷相反啊..
然后for table 的 childeNodes 的 length
结帖了.再次感谢 xingqiliudehuanghun
竟然说ById根本做不到那说明你还没有那种思路