我如何 select 不包含部分数据属性值的元素?
How can I select elements that don't include part of data- attribute value?
我尝试隐藏不包含数据属性中文本部分的元素。
这是一个例子。预期的结果应该是隐藏所有不包含类似于 "UK"
的键的元素
<input type="text" id="step3LRGender" data-country="UK IT">
<input type="text" id="step3LRAge" data-country="PL IT">
$('[data-country*="UK"]').show();
$('[data-country!*="UK"]').hide();
将您的代码与 :not()
合并,如 $('input:not([data-country*="UK"])').hide();
演示
$('input[data-country*="UK"]').show();
$('input:not([data-country*="UK"])').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="step3LRGender" data-country="UK IT">
<input type="text" id="step3LRAge" data-country="PL IT">
您还可以使用以下内容:
$('input,div').filter(function() {
return $(this).data('country').indexOf("UK") > -1 ? $(this).show() : $(this).hide();
});
演示
$('input,div').filter(function() {
return $(this).data('country').indexOf("UK") > -1 ? $(this).show() : $(this).hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="step3LRGender" data-country="UK IT">
<input type="text" id="step3LRAge" data-country="PL IT">
<div data-country="UK IT">UK IT</div>
<div data-country="PL IT">PL IT</div>
我尝试隐藏不包含数据属性中文本部分的元素。 这是一个例子。预期的结果应该是隐藏所有不包含类似于 "UK"
的键的元素<input type="text" id="step3LRGender" data-country="UK IT">
<input type="text" id="step3LRAge" data-country="PL IT">
$('[data-country*="UK"]').show();
$('[data-country!*="UK"]').hide();
将您的代码与 :not()
合并,如 $('input:not([data-country*="UK"])').hide();
演示
$('input[data-country*="UK"]').show();
$('input:not([data-country*="UK"])').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="step3LRGender" data-country="UK IT">
<input type="text" id="step3LRAge" data-country="PL IT">
您还可以使用以下内容:
$('input,div').filter(function() {
return $(this).data('country').indexOf("UK") > -1 ? $(this).show() : $(this).hide();
});
演示
$('input,div').filter(function() {
return $(this).data('country').indexOf("UK") > -1 ? $(this).show() : $(this).hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="step3LRGender" data-country="UK IT">
<input type="text" id="step3LRAge" data-country="PL IT">
<div data-country="UK IT">UK IT</div>
<div data-country="PL IT">PL IT</div>