How to get value inside td using Dom
<html>
<head>
</head>
<body>
<div id="content_Data"></div>
<script type="text/javascript">
function auto()
{
alert("auto called");
document.getElementById('content_Data').innerHTML='<div><table><tr><td>10</td><td>20</td></tr></table></div>';
alert(document.getElementById('content_Data').innerHTML);
getelements();
}
function getelements(){
var searchElement=document.getElementById('content_Data').getElementsByTagName("div");
for( var i=0; i<searchElement.length; i++ )
{
var child_length=searchElement[i].childNodes.length;
for( j=0; j<child_length; j++ )
{
alert(searchElement[i].childNodes[j].nodeValue);
}
}
}
</script>
<sc开发者_如何学运维ript>auto();</script>
</body>
</html>
try looking at innerHTML of td node. Or if you want only text, then it is innerText for IE and textContent for others.
alert(searchElement[i].childNodes[j].innerHTML)
also, jQuery will greatly simplify your code.
Well, just navigate there. For example
document.getElementById('content_Data').firstChild.firstChild.firstChild
gets you the first table cell (with value 10).
You can change the getElementsByTagName("div")
to get the td
elements:
var searchElement=document.getElementById('content_Data').getElementsByTagName("td");
And, it's better to declare the j
variable on the second for
:D. That's all you need to change
精彩评论