简体   繁体   English

我怎么能得到所有 <tr> -s来自 <tbody> 并将其转换为String?

[英]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.

相关问题 如何使用javascript选择嵌套在表的tbody内的tr内的所有锚标记href URL - how can I select all anchor tag href URL that is nested inside a tr inside tbody of a table with javascript 如何获得被点击的表格行的TBODY? - How can I get a clicked table row's TBODY? 去掉 <tr> 我附加到 <tbody> - Remove <tr>s that I appended to a <tbody> 如何从tbody jquery中找到单击的tr - how to find clicked tr from tbody jquery 获取ID,tbody,tr,title中的所有全局属性值 - Get all global attribute values within an Id, tbody, tr, title 如何避免追加其余的 <td> 除了第一个来自新的 <tr> 附加到 <tbody> 使用jQuery? - How to avoid appending rest of the <td>s except first one from a new <tr> appended to the <tbody> using jQuery? 如何使用javascript/jquery循环tbody的第一个tr中的所有单元格? - How to loop all the cell in the first tr of tbody with javascript/jquery? 如何获得第一个id <tr> 的 <tbody> 在HTML表格中使用jQuery? - How to get the id of first <tr> of <tbody> in HTML table using jQuery? 如何从最接近的“ tr”的第n个元素中获取值? - how can I get a value from the nth element of the closest 'tr'? 如何在 Chrome 控制台中的 HTML 元素(如 tbody&gt; tr &gt; td)上触发 dbclick 事件? 我需要通过 javascript 代码来完成(用于过程自动化) - How to fire dbclick event on HTML element (like tbody> tr > td) in Chrome's Console? I need to do it by javascript code (for process automation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM