我正在尝试使用jQuery从一串HTML中过滤出不同的HTML标签。下面是我的代码现在的样子:
$.get("check-in.php", {value: inputValue}).done(function(data) {
var checkinSuccessStr = $("<p/>", { html: data }).find("#success").html();
var checkinFailStr = $("<p/>", { html: data }).find("#fail").html();
var tr = $("<tr/>", { html: data }).find("tr").html();
console.log(checkinSuccessStr); // >> Success String
console.log(checkinSuccessStr); // >> Failure String
console.log(tr); // >> undefined
}
其中check-in.php
将呈现以下超文本标记语言:
"<p id='success'>Success String</p>
<p id='fail'>Failure String</p>
<tr><td>Table cell</td><td>Another cell</td></tr>"
在jQuery中获取checkinSuccessStr
和checkinFailStr
可以很好地工作,但不能获取表行。我曾尝试对生成的tr
变量使用$.parseHTML
,但没有成功。要从HTML文本中获取<tr>
元素(或数组或<tr>
形式的元素),需要做些什么?
发布于 2018-07-11 07:10:04
对于解析损坏的表字符串,使用选择器'>'
,如下所示:
var part_table_str='<tr><td>Table cell</td><td>Another cell</td></tr>';
var cell_counter=0; //only for demonstration
$(part_table_str).find('>').each(function(){
console.log('cell '+cell_counter+' => '+$(this).html());
cell_counter++;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
https://stackoverflow.com/questions/51274372
复制相似问题