[英]How can I get all <tr>-s from a <tbody> and convert it to String?
I have a HTML Table: 我有一个HTML表格:
<table id="persons" border="1">
<thead id="theadID">
<tr>
<th>Name</th>
<th>sex</th>
<th>Message</th>
</tr>
</thead>
<tbody id="tbodyID">
<tr>
<td>Viktor</td>
<td>Male</td>
<td>etc</td>
</tr>
<tr>
<td>Melissa</td>
<td>Female</td>
<td>etc</td>
</tr>
<tr>
<td>Joe</td>
<td>Male</td>
<td>etc</td>
</tr>
</tbody>
</table>
<input type="button" onclick="getTbodyString();" value="get it"/>
I want some jquery/javascript, which can get the inside of the tbody by String. 我想要一些jquery / javascript,它可以通过String获取tbody的内部。 Like: 喜欢:
function getTbodyString() {
var tbodyString = $(.......).text(); //or val()?
alert("the body - "+tbodyString);
}
The result in the alert window should be this: 警报窗口中的结果应为:
the body - <tr><td>Viktor</td><td>Male</td><td>etc</td></tr><tr><td>Melissa</td><td>Female</td><td>etc</td></tr><tr><td>Joe</td><td>Male</td><td>etc</td></tr>
Could anyone help me? 谁能帮助我?
You need to html() instead of text(). 你需要html()而不是text()。 The function text will not return you tags but returns plain text within tags. 函数文本不会返回标签,而是返回标签内的纯文本。
var tbodyString = $('#tbodyID').html();
I would use native javascript function here that would be faster then jquery. 我会在这里使用原生的javascript函数,这将比jquery更快。
var tbodyString = document.getElementById('tbodyID').innerHTML;
Just Replace your function code as below : 只需替换您的功能代码如下:
function getTbodyString() {
var tbodyString = $('#tbodyID').html();
alert("the body - "+tbodyString);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.