gpt4 book ai didi

jquery - 从文本中删除具有特定类的所有元素

转载 作者:行者123 更新时间:2023-11-28 01:19:00 24 4
gpt4 key购买 nike

如何从文本中删除所有具有特定类的 DOM 元素?

var html = "<table>"+
"<tr>"+
"<td>1</td>"+
"<td>2</td>"+
"<td class='del'>3</td>"+
"</tr>"+
"<tr>"+
"<td>1</td>"+
"<td>2</td>"+
"<td class='del'>3</td>"+
"</tr>"+
"</table>";

最佳答案

您可以将 HTML 转换为 DOM 对象,删除指定的项目,并取回其 HTML:

var html = "<table>" +
"<tr>" +
"<td>1</td>" +
"<td>2</td>" +
"<td class='del'>3</td>" +
"</tr>" +
"<tr>" +
"<td>1</td>" +
"<td>2</td>" +
"<td class='del'>3</td>" +
"</tr>" +
"</table>";

var $obj = $(html); // Create jQuery object
$obj.find(".del").remove(); // Remove .del items
var htmlNew = $obj[0].outerHTML; // Get the updated HTML

// Demonstration purposes only:
$("body").text(htmlNew);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于jquery - 从文本中删除具有特定类的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com