隐藏由多数据属性与现有变量匹配 jQuery/JavaScript
hide by multi data attributes match existing variables with jquery/javascript
我试图隐藏一个具有两个html数据属性的div,例如下面的一个具有工程师和纽约的html数据属性
<div class="row" data-job-type="Engineer" data-job-location="New York"></div>
<div class="row" data-job-type="Lockmsith" data-job-location="Maryland"></div>
<div class="row" data-job-type="Wizard" data-job-location="New York"></div>
下面我尝试以这种方式传递它,但是当我向其添加第二个属性时它不起作用。
var bar = "New York";
var foo = "Engineer";
$('.row[data-job-type=' + foo + ']','.row[data-job-location=' + bar + ']').hide();
由于属性值中包含空格,因此必须将其括在"
或'
中。
另外,由于您要选择具有两个属性的元素,因此需要按如下方式组合它们
var bar = "New York";
var foo = "Engineer";
$('.row[data-job-type="' + foo + '"][data-job-location="' + bar + '"]').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" data-job-type="Engineer" data-job-location="New York">1</div>
<div class="row" data-job-type="Lockmsith" data-job-location="Maryland">2</div>
<div class="row" data-job-type="Wizard" data-job-location="New York">3</div>
<html>
<head>
<title>sample Page</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$('.row[data-job-type="Engineer"][data-job-location="New York"]').hide();
});
</script>
</head>
<body>
<div class="row" data-job-type="Engineer" data-job-location="New York">1111</div>
<div class="row" data-job-type="Lockmsith" data-job-location="Maryland">22222</div>
<div class="row" data-job-type="Wizard" data-job-location="New York">333333</div>
</body>
</html>
相关文章:
- jQuery/Javascript.toggleClass/.classList.toggle的特殊行为
- 学生搜索项目jquery/javascript
- jQuery/JavaScript在线公文包表单-打印样式表
- 如何在页面加载中使用Jquery/Javascript确定img源
- DataTables合并了两个独立表中的jQuery/Javascript函数
- 在 Jquery/Javascript 中使用多个 OR (||) 运算符时如何设置变量
- 如何在jquery+javascript中只在输入框中输入数字(使用regex)
- 在Jquery/Javascript中的Array中插入Textbox值
- 使用Jquery/Javascript替换CSS属性
- Jquery/Javascript:通过按下按钮显示数组中的随机图像
- 选中复选框的Jquery/Javascript问题使用输入框操作将行从一个表添加到另一个表
- 在for循环中未获取JQuery/JavaScript对象值
- 使用jQuery/Javascript在Facebook上发送消息
- jQuery/Javascript foreach If/Else |如果为空(不执行任何操作)Else(显示forea
- 使用jQuery+Javascript识别一个空文本节点
- jquery/javascript字符串中的撇号
- jQuery/Javascript在内容可编辑的当前段落后插入元素
- 在页面加载时激活jQuery/Javascript函数
- 如何创建php函数或任何脚本/jquery Javascript,使我能够获取与复选框关联的值并添加它们
- Jquery/Javascript使用IF/ELSE语句更改img SRC