我正在尝试编写一个页面上有数千个图像缩略图的代码,并且用户希望快速且重复地搜索它们。
因此转向值得信赖的 jQuery。我想出了以下代码:
$(document).ready(function () {
$('input[name="search"]').keyup(function(e){
console.log('checking...');
var $this = $(this);
if($this.val().length > 3 || e.which == 8 || e.which == 46) {
var check = 'img[title*="' + $this.val() + '"]';
$.each($('.asset'), function() {
$(this).show();
$(this).not(check).hide();
})
console.log(check);
} else {
console.log('False');
};
});
});
这仅部分有效。它失败的地方是 $(this).not(check).hide();
只是隐藏了所有内容,而不只选择标题中带有搜索查询的图像。
有没有办法让jQuery来完成这个任务?
最佳答案
$('input[name="search"]').keyup(function(e){
console.log('checking...');
var search = $(this).val();
if(search.length > 3 || e.which == 8 || e.which == 46) {
$(".asset img").hide();
$(".asset img[title*='" + search + "']").show();
} else {
console.log('False');
};
});
您的选择器的问题在于 $(this).not(check)
检查 $(this)
是否与 check
选择器匹配,但我猜测 .asset
是包含 IMG 的 DIV,而不是图像元素本身。
关于javascript - jQuery 隐藏标题中没有 $somestuff 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489653/