我有一个表,我想将HTML指定为字符串变量。我该怎么做才能打开/关闭table
标签包含在字符串中?我知道我可以将表包装在容器中并获取容器的HTML,但是想知道是否有解决方案而不修改布局。
$(function() {
// want the following variable to include open/closing table tags
var tableHTML = $('table').html();
alert(tableHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
答案 0 :(得分:1)
使用outerHTML
$(function() {
// want the following variable to include open/closing table tags
var tableHTML = $('table')[0].outerHTML;
console.log(tableHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
答案 1 :(得分:1)
试试这个。
$(function() {
// want the following variable to include open/closing table tags
var tableHTML = $('table').prop('outerHTML')
alert(tableHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>