如何在data-attr中查找数值?

huangapple go评论64阅读模式
英文:

How to find Value in data-attr?

问题

我的英语不太好,抱歉。

我在显示中有带重音的单词。
但我希望能够搜索没有重音和附加项的单词。

我想在 data-search 中查找文本。

我想要类似以下的 data-search:

$("[data-search]:contains(" + text + ")")

例如:$("[data-search]:contains(" + text + ")")(但这不太有效)

如果搜索 À plus,是可以的。

如果搜索 A-plus,找不到。

我想搜索 A-plusAPlus...

英文:

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+&quot;:contains(&quot; + text + &quot;)&quot;)
        .filter(function(){return $(this).text().replace(/#/i,&#39;&#39;);})
        .clone().get();
    return find;

}
$(&quot;.searcher&quot;).on(&#39;keyup change&#39;,function(){
$(&quot;.result&quot;).html(&#39;&#39;)
  var value=$(this).val().trim().replace(/#/i,&#39;&#39;);
  var listItems=&quot;.items&quot;;
  var found=findElementByText(listItems,value);
  if(found!=&#39;&#39;)
      $(&quot;.result&quot;).html(found);
  else
      $(&quot;.result&quot;).html(&#39;&lt;i&gt;Not found!&lt;/i&gt;&#39;);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;!--search:--&gt;
&lt;input class=&quot;searcher&quot;&gt;
    
    
&lt;ul&gt;    
&lt;!--items:--&gt;
&lt;li class=&quot;items&quot; data-search=&quot;A-plus&quot;&gt;#&#192; plus !&lt;/li&gt;
&lt;/ul&gt;
   
&lt;!--result:--&gt;
result:&lt;br&gt;
&lt;div class=&quot;result&quot;&gt;&lt;/div&gt;

<!-- 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.jsList.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=&quot;${text}&quot;]`);
  if (find.length) {
    return find.clone();
  }
  return &#39;&lt;i&gt;Not found!&lt;/i&gt;&#39;;
}

$(document).on(&#39;input&#39;, &#39;.searcher&#39;, function() {
  $(&#39;.result&#39;).html( findElementByText( &#39;.items&#39;, $(this).val() ) );
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input class=&quot;searcher&quot;&gt;
&lt;ul&gt;    
	&lt;li class=&quot;items&quot; data-search=&quot;A-plus&quot;&gt;#&#192; plus !&lt;/li&gt;
&lt;/ul&gt;
result:&lt;br&gt;
&lt;div class=&quot;result&quot;&gt;&lt;/div&gt;

<!-- 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 = [];

$(&#39;.items&#39;).each(function() {
  list.push({
    title: $(this).data(&#39;search&#39;),
    el: $(this).clone()
  });
});

const fuse = new Fuse(list, {
  shouldSort: false,
  threshold: .5,
  keys: [&#39;title&#39;]
});

$(document).on(&#39;input&#39;, &#39;.searcher&#39;, function() {
  const results = fuse.search( $(this).val() );
  $(&#39;.result&#39;).html(&#39;&#39;);
  if(results.length) {
    results.map( o =&gt; $(&#39;.result&#39;).append(o.item.el) );
  } else {
    $(&#39;.result&#39;).html(&#39;&lt;i&gt;Not found!&lt;/i&gt;&#39;);
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/fuse.js@6.6.2&quot;&gt;&lt;/script&gt;
&lt;input class=&quot;searcher&quot;&gt;
&lt;ul&gt;    
  &lt;li class=&quot;items&quot; data-search=&quot;A-plus&quot;&gt;#&#192; plus !&lt;/li&gt;
  &lt;li class=&quot;items&quot; data-search=&quot;A-minus&quot;&gt;#&#192; minus !&lt;/li&gt;
&lt;/ul&gt;
&lt;br&gt;
result:&lt;br&gt;
&lt;ul class=&quot;result&quot;&gt;&lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月28日 17:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定