gpt4 book ai didi

Javascript 搜索栏仅搜索标题

转载 作者:行者123 更新时间:2023-11-30 15:33:00 26 4
gpt4 key购买 nike

如何只搜索 b 标签而不搜索整个 div。现在我可以搜索整个 div,但我只想搜索 b 标签中的标题。如果列表中有名称,则必须将其删除HTML :

<div class="row">
<div class="col-md-3">
<div class="panel panel-default" id="list">
<div class="panel-header">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for users" id="searchName"/>
<div class="input-group-btn">
<div class="btn-group">
<button type="button" class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span></button>
</div>
</div>
</div>
</div>
<div class="panel-body" id="user1">
<div class="row">
<div class="col-md-4">
<img src="" class="img-thumbnail">
</div>
<div class="col-md-8">
<b>Pieter</b><br>
(Laatste bericht)<br>
<i>12:01</i>
</div>
</div>
</div>
<div class="panel-body" id="user2">
<div class="row">
<div class="col-md-4">
<img src="" class="img-thumbnail">
</div>
<div class="col-md-8">
<b>Karel</b><br>
(Laatste bericht)<br>
<i>11:15</i>
</div>
</div>
</div>
<div class="panel-body" id="user3">
<div class="row">
<div class="col-md-4">
<img src="" class="img-thumbnail">
</div>
<div class="col-md-8">
<b>Pilot</b><br>
(Laatste bericht)<br>
<i>9:36</i>
</div>
</div>
</div>
</div>
</div>

Javascript:

function contains(text_one, text_two){
if (text_one.indexOf(text_two) != -1){
return true;
}
}

$("#searchName").keyup(function () {
var searchName = $("#searchName").val().toLowerCase();
$(".panel-body").each(function () {
if(!contains($(this).text().toLowerCase(), searchName)) {
$(this).hide();
}
else {
$(this).show();
}
});
});

最佳答案

在条件中使用find()来关注元素的子B标签:

之前:

if(!contains($(this).text().toLowerCase(), searchName)) {

之后:

if(!contains($(this).find('b').text().toLowerCase(), searchName)) {

关于Javascript 搜索栏仅搜索标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979653/

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