英文:
How to find Value in data-attr?
问题
我的英语不太好,抱歉。
我在显示中有带重音的单词。
但我希望能够搜索没有重音和附加项的单词。
我想在 data-search 中查找文本。
我想要类似以下的 data-search:
$("[data-search]:contains(" + text + ")")
例如:$("[data-search]:contains(" + text + ")")
(但这不太有效)
如果搜索 À plus
,是可以的。
如果搜索 A-plus
,找不到。
我想搜索 A-plus
或 A
或 Plus
...
英文:
my english is not good, sorry.
I have accented words in the display.
But I want the word to be searched without accents and additional items.
i want find text in data-search=""
I want something like this for data-search:
$(elem+":contains(" + text + ")")
eg.: $("[data-search]:contains(" + text + ")")
(But this does not work very well)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function findElementByText(elem,text) {
var find = $(elem+":contains(" + text + ")")
.filter(function(){return $(this).text().replace(/#/i,'');})
.clone().get();
return find;
}
$(".searcher").on('keyup change',function(){
$(".result").html('')
var value=$(this).val().trim().replace(/#/i,'');
var listItems=".items";
var found=findElementByText(listItems,value);
if(found!='')
$(".result").html(found);
else
$(".result").html('<i>Not found!</i>');
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--search:-->
<input class="searcher">
<ul>
<!--items:-->
<li class="items" data-search="A-plus">#À plus !</li>
</ul>
<!--result:-->
result:<br>
<div class="result"></div>
<!-- end snippet -->
if search À plus, is ok.
if search A-plus, is not found.
i want search A-plus or A or Plus...
答案1
得分: 0
以下是根据 data-search
值进行精确搜索的示例:
<!-- 开始代码片段: js 隐藏: true 控制台: true Babel: false -->
<!-- 语言: lang-js -->
function findElementByText(elem, text) {
const find = $(`${elem}[data-search="${text}"]`);
if (find.length) {
return find.clone();
}
return '<i>未找到!</i>';
}
$(document).on('input', '.searcher', function() {
$('.result').html(findElementByText('.items', $(this).val()));
});
<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="searcher">
<ul>
<li class="items" data-search="A-plus">#À plus !</li>
</ul>
result:<br>
<div class="result"></div>
<!-- 结束代码片段 -->
如果您需要模糊搜索,可以使用 Fuse.js 或 List.js 或其他一些库。以下是使用 Fuse.js 的示例:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
const list = [];
$('.items').each(function() {
list.push({
title: $(this).data('search'),
el: $(this).clone()
});
});
const fuse = new Fuse(list, {
shouldSort: false,
threshold: .5,
keys: ['title']
});
$(document).on('input', '.searcher', function() {
const results = fuse.search($(this).val());
$('.result').html('');
if (results.length) {
results.map(o => $('.result').append(o.item.el));
} else {
$('.result').html('<i>未找到!</i>');
}
});
<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.6.2"></script>
<input class="searcher">
<ul>
<li class="items" data-search="A-plus">#À plus !</li>
<li class="items" data-search="A-minus">#À minus !</li>
</ul>
<br>
result:<br>
<ul class="result"></ul>
<!-- 结束代码片段 -->
如果您有其他问题或需要进一步帮助,请告诉我。
英文:
Below is an example of an exact search by data-search
value:
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
function findElementByText(elem, text) {
const find = $(`${elem}[data-search="${text}"]`);
if (find.length) {
return find.clone();
}
return '<i>Not found!</i>';
}
$(document).on('input', '.searcher', function() {
$('.result').html( findElementByText( '.items', $(this).val() ) );
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="searcher">
<ul>
<li class="items" data-search="A-plus">#À plus !</li>
</ul>
result:<br>
<div class="result"></div>
<!-- end snippet -->
If you need a fuzzy-search, you can use Fuse.js, or List.js, or some other librarys.
Here is an example of using Fuse.js:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const list = [];
$('.items').each(function() {
list.push({
title: $(this).data('search'),
el: $(this).clone()
});
});
const fuse = new Fuse(list, {
shouldSort: false,
threshold: .5,
keys: ['title']
});
$(document).on('input', '.searcher', function() {
const results = fuse.search( $(this).val() );
$('.result').html('');
if(results.length) {
results.map( o => $('.result').append(o.item.el) );
} else {
$('.result').html('<i>Not found!</i>');
}
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.6.2"></script>
<input class="searcher">
<ul>
<li class="items" data-search="A-plus">#À plus !</li>
<li class="items" data-search="A-minus">#À minus !</li>
</ul>
<br>
result:<br>
<ul class="result"></ul>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论