gpt4 book ai didi

javascript - 搜索数据名​​称属性内容,如果匹配则显示

转载 作者:行者123 更新时间:2023-11-30 12:21:34 25 4
gpt4 key购买 nike

我想搜索名为“data-name”的属性的内容,该属性嵌入在具有“infodata”类的每个 div 中,就像您在下面看到我的 fiddle 一样,有 3 个 div,每个都有一个“如果我要进入输入框(搜索框),例如“J 或 j”或任何字母或完整名称,例如“Jason”或全名,例如“Mechelle Hill”,只要该输入框(搜索输入框)上的任何内容与任何“data-name”属性内容匹配,就会显示匹配的具有“infodata”类的 div,否则隐藏它。任何建议、建议、想法、线索、帮助,将不胜感激。谢谢!

$(document).ready(function(){

//empty yet, dont know how to do it :(

});
.extend{max-width: 100%; max-height: 100%;}
.clear{clear: both; float: none;}
.bgwhite{background: #ffffff;}
.center{margin-left: auto; margin-right: auto;}
.display_block{display: block;}
.align_left{float: left;}
.align_right{float: right;}

#searchbox_container input[type="text"]{
bacground: #ffffff;
border: 1px solid #cccccc;
padding: 8px;
color: #555555;
}
#searchbox_container input[type="text"]:focus{
outline: none;
}
.infodata{
margin: 5px;
}
.infodata h1{
font-size: 15px;
padding: 0px;
margin: 0px 0px 3px 0px;
text-align: center;
text-transform: uppercase;
}
.infodata p{
font-size: 10px;
padding: 0px;
margin: 0px;
text-align: center;
text-transform: uppercase;
}
.infodata img{
width: 125px;
height: 125px;
margin-bottom: 5px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="extend clear bgwhite extend center" id="mainwrapper">
<div class="extend clear" id="searchbox_container">
<input type="text" name="searchnow" placeholder="Search here..." value="" class="extend clear display_block align_right" />
</div> <!-- end of searchbox_container -->
<div class="extend clear" id="info_data_container">
<div class="infodata extend display_block align_left" data-name="Jason Acapela">
<img src="img/user1.jpg" class="extend clear display_block center" alt="" width="125" height="125" title="Jason Acapela">
<h1>Jason Acapela</h1>
<p>Web Developer</p>
</div> <!-- end of .infodata -->
<div class="infodata extend display_block align_left" data-name="Derrick Tour">
<img src="img/user1.jpg" class="extend clear display_block center" alt="" width="125" height="125" title="Derrick Tour">
<h1>Derrick Tour</h1>
<p>UI/UX</p>
</div> <!-- end of .infodata -->
<div class="infodata extend display_block align_left" data-name="Mechelle Hill">
<img src="img/user1.jpg" class="extend clear display_block center" alt="" width="125" height="125" title="Mechelle Hill">
<h1>Mechelle Hill</h1>
<p>System Developer</p>
</div> <!-- end of .infodata -->
</div> <!-- end of #info_data_container -->
</div> <!-- end of #mainwrapper -->

最佳答案

  1. 使用 input 事件捕获对搜索的更改
  2. 默认隐藏所有.infodata div
  3. 如果搜索值与属性匹配,则显示。您可以使用 indexOf()
  4. 检查
  5. 确保使用 toUpperCasetoLowerCase 使搜索不区分大小写

$(document).ready(function(){
$('[name="searchnow"]').on('input', function(){
var that = $(this);
$('.infodata').hide();
$('.infodata').each(function(){
if($(this).attr('data-name').toUpperCase().indexOf(that.val().trim().toUpperCase()) > -1){
$(this).show();
}
});
});
});
.extend{max-width: 100%; max-height: 100%;}
.clear{clear: both; float: none;}
.bgwhite{background: #ffffff;}
.center{margin-left: auto; margin-right: auto;}
.display_block{display: block;}
.align_left{float: left;}
.align_right{float: right;}

#searchbox_container input[type="text"]{
bacground: #ffffff;
border: 1px solid #cccccc;
padding: 8px;
color: #555555;
}
#searchbox_container input[type="text"]:focus{
outline: none;
}
.infodata{
margin: 5px;
}
.infodata h1{
font-size: 15px;
padding: 0px;
margin: 0px 0px 3px 0px;
text-align: center;
text-transform: uppercase;
}
.infodata p{
font-size: 10px;
padding: 0px;
margin: 0px;
text-align: center;
text-transform: uppercase;
}
.infodata img{
width: 125px;
height: 125px;
margin-bottom: 5px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="extend clear bgwhite extend center" id="mainwrapper">
<div class="extend clear" id="searchbox_container">
<input type="text" name="searchnow" placeholder="Search here..." value="" class="extend clear display_block align_right" />
</div> <!-- end of searchbox_container -->
<div class="extend clear" id="info_data_container">
<div class="infodata extend display_block align_left" data-name="Jason Acapela">
<img src="img/user1.jpg" class="extend clear display_block center" alt="" width="125" height="125" title="Jason Acapela">
<h1>Jason Acapela</h1>
<p>Web Developer</p>
</div> <!-- end of .infodata -->
<div class="infodata extend display_block align_left" data-name="Derrick Tour">
<img src="img/user1.jpg" class="extend clear display_block center" alt="" width="125" height="125" title="Derrick Tour">
<h1>Derrick Tour</h1>
<p>UI/UX</p>
</div> <!-- end of .infodata -->
<div class="infodata extend display_block align_left" data-name="Mechelle Hill">
<img src="img/user1.jpg" class="extend clear display_block center" alt="" width="125" height="125" title="Mechelle Hill">
<h1>Mechelle Hill</h1>
<p>System Developer</p>
</div> <!-- end of .infodata -->
</div> <!-- end of #info_data_container -->
</div> <!-- end of #mainwrapper -->

关于javascript - 搜索数据名​​称属性内容,如果匹配则显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30851335/

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