1 、 JavaScript 方法 : Document.getElementById ("id"). Innerhtml;
(1 )实例说明
复制代码代码如下:
<! DocType html public "-// w3c // dtd xhtml 1.0 Transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transicional.dtd ">
<html xmlns = "http://www.w3.org/1999/xhtml">
<Evista>
<meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<title> 获取 html 中的子项 </title>
<script type = "text/javaScript">
función getTr ()
{
var str = document.getElementById ("div_child"). innerhtml;
alerta (str);
}
</script>
</ablo>
<Body>
<div id = "div_child">
<Table>
<tr>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
</tr>
<tr>
<TD> </td>
<TD> </td>
<TD> </td>
<TD> </td>
<TD> </td>
</tr>
</table>
<input type = "botón" id = "btn" onClick = "getStr ()" value = "获取字符串"/>
</div>
</body>
</html>
(2 )点击按钮后 弹出信息 弹出信息
2 、 jQuery 中的方法 : $ ("#id"). Html ()
(1 )实例说明
复制代码代码如下:
<! DocType html public "-// w3c // dtd xhtml 1.0 Transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transicional.dtd ">
<html xmlns = "http://www.w3.org/1999/xhtml">
<Evista>
<meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<title> jQuery 中父节点获取子节点字符串 </title>
<script type = "text/javaScript" src = "jQuery-2.1.0.js"> </script>
<script type = "text/javaScript">
$ (function () {
$ ("#btn"). Click (function () {
var str = $ ("#div_child"). html ();
alerta (str);
});
});
</script>
</ablo>
<Body>
<div id = "div_child">
<Table>
<tr>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
</tr>
<tr>
<TD> </td>
<TD> </td>
<TD> </td>
<TD> </td>
<TD> </td>
</tr>
<tr>
<TD> </td>
<TD> </td>
<TD> </td>
<TD> </td>
<TD> </td>
</tr>
<tr>
<TD> </td>
<TD> </td>
<TD> </td>
<TD> </td>
<TD> </td>
</tr>
</table>
<input type = "botón" id = "btn" value = "获取值"/>
</div>
</body>
</html>
(2 )运行结果