gpt4 book ai didi

javascript - 按标题属性过滤元素

转载 作者:行者123 更新时间:2023-11-30 14:04:07 25 4
gpt4 key购买 nike

我正在使用来自 SO - jQuery - Search image title attribute 的解决方案.

这是该解决方案的演示:

/*
* Plugin Name: QuickFilter
* Author: Collin Henderson (collin@syropia.net)
* Version: 1.0
* © 2012, http://syropia.net
* You are welcome to freely use and modify this script in your personal and commercial products. Please don't sell it or release it as your own work. Thanks!
* Seach images by title https://stackoverflow.com/questions/42530073/jquery-search-image-title-attribute/42531782#42531782
*/
(function($){
$.extend($.expr[':'], {missing: function (elem, index, match) {
return (elem.textContent || elem.innerText || elem.title || "").toLowerCase().indexOf(match[3]) == -1;
}});
$.extend($.expr[':'], {exists: function(elem, i, match, array){
return (elem.textContent || elem.innerText || elem.title || '').toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}});
$.extend($.fn,{
quickfilter: function(el){
return this.each(function(){
var _this = $(this);
var query = _this.val().toLowerCase();
_this.keyup(function () {
query = $(this).val().toLowerCase();
if(query.replace(/\s/g,"") != ""){
$(el+':exists("' + query.toString() + '")').show();
$(el+':missing("' + query.toString() + '")').hide();
}
else {
$(el).show();
}
});
});
}
});
})(jQuery);

$(document).ready(function(){
$('#txtSearch').quickfilter('#list img');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<div class="container">

<div id="list">
<input id="txtSearch" placeholder="filter" style="border:1px solid silver; display:block; margin:5px;" type="text">
<img title="grapes" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f347.svg" style="width:30px; height:30px">
<img title="watermelon" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f349.svg" style="width:30px; height:30px">
<img title="tangerine" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f34a.svg" style="width:30px; height:30px">
<img title="lemon" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f34b.svg" style="width:30px; height:30px">
<img title="banana" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f34c.svg" style="width:30px; height:30px">
</div>

</div>

此解决方案非常适合根据标题属性搜索图像 - 正如 SO 问题的标题所述。

我希望能够根据 span 元素的 title 属性进行搜索 - 例如

<div id="listnature">
<input type="text" id="txtSearchnature" placeholder="Filter Animals & Nature" class="form-control" />
<span title="monkey face - 🐵">🐵</span>
<span title="monkey - 🐒">🐒</span>
<span title="gorilla - 🦍">🦍</span>
<span title="dog face - 🐶">🐶</span>
<span title="dog - 🐕">🐕</span>
<span title="poodle - 🐩">🐩</span>
<span title="wolf face - 🐺">🐺</span>
</div>

将相关的JS改为:

$(document).ready(function(){
$('#txtSearchnature').quickfilter('#listnature span');
});

但是 - 过滤器不起作用,因为 JS 基于基于图像标题属性的过滤解决方案 - 在这种情况下,我想在 span 的标题属性上进行过滤。

我想知道是否可以更改 JS 来实现该结果?

最佳答案

问题出在这一行

return (elem.textContent || elem.innerText || elem.title || "")

它说:

获取textContent,如果为空,则获取innerText,如果为空,则获取title,否则为空字符串。

spanimg 的区别在于 span 有一个 textContent 所以它给出了 textContent(unicode 图像)并尝试匹配它 - 但失败了。

如果您希望首先匹配标题,您可以更改检查顺序:

return (elem.title || elem.textContent || elem.innerText || "")

您需要执行此操作两次,一次用于 :missing 一次用于 :exists - 当然您实际上只需要其中之一并通过隐藏/显示默认。

更新代码:

/*
* Plugin Name: QuickFilter
* Author: Collin Henderson (collin@syropia.net)
* Version: 1.0
* © 2012, http://syropia.net
* You are welcome to freely use and modify this script in your personal and commercial products. Please don't sell it or release it as your own work. Thanks!
* Seach images by title https://stackoverflow.com/questions/42530073/jquery-search-image-title-attribute/42531782#42531782
*/
(function($){
$.extend($.expr[':'], {missing: function (elem, index, match) {
return (elem.title || elem.textContent || elem.innerText || "").toLowerCase().indexOf(match[3]) == -1;
}});
$.extend($.expr[':'], {exists: function(elem, i, match, array){
return (elem.title || elem.textContent || elem.innerText || '').toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}});
$.extend($.fn,{
quickfilter: function(el){
return this.each(function(){
var _this = $(this);
var query = _this.val().toLowerCase();
_this.keyup(function () {
query = $(this).val().toLowerCase();
if(query.replace(/\s/g,"") != ""){
$(el+':exists("' + query.toString() + '")').show();
$(el+':missing("' + query.toString() + '")').hide();
}
else {
$(el).show();
}
});
});
}
});
})(jQuery);

$(document).ready(function(){
$('#txtSearchnature').quickfilter('#listnature span');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="listnature">
<input type="text" id="txtSearchnature" placeholder="Filter Animals & Nature" class="form-control" />
<span title="monkey face - 🐵">🐵</span>
<span title="monkey - 🐒">🐒</span>
<span title="gorilla - 🦍">🦍</span>
<span title="dog face - 🐶">🐶</span>
<span title="dog - 🐕">🐕</span>
<span title="poodle - 🐩">🐩</span>
<span title="wolf face - 🐺">🐺</span>
</div>

关于javascript - 按标题属性过滤元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55784704/

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