上代码...请问我 document.getElementByName("show")可以取到c标签对吧 但现在我想取a标签的style元素的值 我ok方法应该怎么写 <c id =<%= i%> name="show">
<b id=<%=m%>>
<a href="#" style="abc">
</a>
</b>
</c><script type=text/javascript>
function ok() {
document.getElementByName("show"). }
</script>
<b id=<%=m%>>
<a href="#" style="abc">
</a>
</b>
</c><script type=text/javascript>
function ok() {
document.getElementByName("show"). }
</script>
不过老衲最近喜欢jquery
jquery:
$(c a)[0].style
function ok(){
var style= document.getElementsByName("show")[0].getElementsByTagName('a')[0].getAttribute('style');
alert(style);
}
<!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" />
<title></title><script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script>
$(function(){
alert($('c a')[0].style.color)
})</script></head><body>
<c id ='' name="show">
<b id=''>
<a href="#" style="color:red;">1121
</a>
</b>
</c>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>?</title>
<style type="text/css">
div{float:left;border:#000 solid 1px;width:100px;height:100px;}
</style>
</head>
<body>
<c name="show" style="dsadsadsa">
<b>
<a id ="name" href="#" style="abc" onclick="ok()">asdasdasdasd
</a>
</b>
</c><script type=text/javascript>
function ok(){
var style= document.getElementsByName("show")[0].getElementsByTagName('a')[0].getAttribute('style');
alert(style);
}
</script></body>
</html>
<html>
<head>
<title></title> <script>
function ok() {
var style = document.getElementsByName("show")[0].getElementsByTagName('a')[0].getAttribute('style');
alert(style);
}
</script></head>
<body>
<input type='button' value='button' onclick="ok()" />
<c name="show">
<b >
<a href="#" style="abc">
</a>
</b>
</c>
</body>
</html>
楼主请注意,我回复的都是在本地测试过在发上来的.
呈上我的测试代码.
function ok(style){
alert(style);
}
我是在chrome里测试的.
在IE里确实不可以.因为IE是不兼容getElementsByName方法的.所以楼主尽量用ID来索引Element吧.
附上源代码
<!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>
<title>find a style Page</title>
</head><script type="text/javascript" language="javascript">
function getStyle() {
var style="abc";
// style=""; 我想找到a标签的style属性,请html之父老胡提供想法
var a=document.getElementsByTagName("a")[0];
alert(a.getAttribute("style").cssText||a.getAttribute("style"));
}
</script><body>
<div name="show">
<b>
<a href="#" style="background:#ffffff;">abc</a>
</b>
</div>
<input type='button' value='button' onclick="getStyle()" /></body>
</html>
但是楼主想要的是从一个容器里('c')查找一个标签('a').应该先找到这个容器在去查找容器里面的element啊.你这个不符合楼主的意思啊.