javascript - 如何使用 jquery 从 div 部分中删除特定值
问题描述
我创建了一个包含动态标签的 div。我正在尝试使用 jquery 从 div 部分中删除特定值。如果字符串在 div 部分字符串中匹配,我已经声明了包含字符串的变量,它应该从 div 中删除。有没有办法做到这一点?
<div class="VS" id="MM" style="border:1px solid gray; width:150px;">
<label class="custom_label_div2" id="lbl1"> XYZ </label>
<label class="custom_label_div2" id="lbl2"> XYZC </label>
<label class="custom_label_div2" id="lbl3"> QWER </label>
<div>
我试图做这样的事情
var str = XYZ; --- this is my varibale which will contain any label deriving from somewhere else it can contain any one of the value i.e. XYZ or XYZC or QWER
$('#MM').each(function() {
var $this = $(this);
if ($this.text() === str) {
$this.remove();
}
});
解决方案
if ($this.text() === str) {
$this.remove();
}
- 这是删除元素本身,如果您需要清空它,请使用:
if ($this.text() === str) {
$this.text("");
}
或者
if ($this.text() === str) {
$this.html(""); // if there are other elements inside
}
要不就
if ($this.text() === str) {
$this.empty();
}
推荐阅读
- java - Oracle Java:将 java.sql.Blob 数据类型转换为 Oracle BLOB 数据类型时出错
- c# - 引用 .NET Standard 类库的 .NET windows 窗体项目和 UWP 项目:
- r - 迭代R中的列表
- python - 将“文件路径”列添加到 pandas DataFrame
- react-native - 如何使用 react-native-svg 模块以编程方式将 SVG 元素添加到组件?
- limit - 为什么A-Frame的人脸限制这么低?
- c# - 我正在尝试建立一个积分系统来确定 4 名玩家之间的获胜者
- informatica - 生成 informatica 映射以节省开发工作量
- c++ - 我想在 CentOS 7 上安装 clang-tidy 作为 cpp 的 linter 但找不到包
- erlang - 生成过程中的 io:getline(Prompt):不显示提示