javascript - 按标题属性过滤元素

标签 javascript jquery html

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

这是该解决方案的演示:

/*
 * Plugin Name: QuickFilter
 * Author: Collin Henderson (<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="52313d3e3e3b3c12212b203d223b337c3c3726" rel="noreferrer noopener nofollow">[email protected]</a>)
 * 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 基于图像标题属性进行过滤的解决方案 - 在这种情况下,我想过滤跨度的标题属性。

我想知道是否可以改变 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 (<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8e8281818483ad9e949f829d848cc3838899" rel="noreferrer noopener nofollow">[email protected]</a>)
 * 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/

相关文章:

javascript - d3.js 甘特图

jquery - 提交前如何检查?

jQuery Mobile 动态添加元素

java - 通过 Javascript 进行 Vertx 3 应用程序日志记录

javascript - 如何使用 jQuery 触发类更改事件?

javascript - Twitter-bootstrap popover 不允许下面的内容起作用

javascript - JQuery .click() 函数在 ajax 调用中不起作用

html - 视频背景全屏响应

css - 带有水平滚动条和粘性标题的大表格

javascript - 添加 ajax 后, knockout 可观察数组未更新