简体   繁体   English

如何在不显示 html 标记的情况下过滤和标记输入中的单词?

[英]How can I filter and mark words from an input without html markup being displayed?

I am trying to filter and mark word from a webpage, and Sajeeb Ahamed graciously assisted me with a piece of code that functions exactly as I wanted,however when I add and other element tags such as an list item or heading tag when I clear the input box it displays the HTML markup.我正在尝试过滤和标记网页中的单词,Sajeeb Ahamed 慷慨地帮助我编写了一段完全符合我要求的代码,但是当我添加其他元素标签(如列表项或标题标签)时,我清除了输入框显示 HTML 标记。

 $(document).ready(function() { $("#myInput").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#myDIV>*").map(function() { var el = $(this); var content = el.html().replace(/(<span class="highlighted">)|(<\/span>)/g, ""); el.html(content); var hasText = el.text().toLowerCase().indexOf(value) > -1; el.toggle(hasText); if (hasText) { // escape value for use in regex var textRegex = new RegExp(value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"); el.html(content.replace(textRegex, '<span class="highlighted">$&</span>')); } }); }); });
 .highlighted { background-color: yellow; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="myInput" /> <!-- the new search --> <div id="myDIV"> <p>This is a test</p> <ul> <li>This is a list item</li> <li>This is a another list item</li> </ul> <a href="">This is a link</a> </div>

This the code, it will only accept a paragraph tag.这个代码,它只会接受一个段落标签。 Does anybody have any idea why?有人知道为什么吗?

Thanks谢谢

It works with any element inside the element with id 'myDIV' at first level (not deep),because you are using this selector $("#myDIV>*") .它适用于第一级(不深)具有 id 'myDIV' 的元素内的任何元素,因为您正在使用此选择器$("#myDIV>*") Be sure that your tag is inside this rule.确保您的标签在此规则内。

UPDATE WITH NEW INFORMATION更新新信息

 $(document).ready(function() { $("#myInput").on("keyup", function() { var value = $(this).val().toLowerCase(); // Remove all class="highlighted" inside #myDIV $("#myDIV").html($("#myDIV").html().replace(/(<span class="highlighted">)|(<\/span>)/g, "")) $("#myDIV *").map(function() { var el = $(this); // Only in deep children aplly your logic if (el.children().length == 0) { var content = el.html().replace(/(<span class="highlighted">)|(<\/span>)/g, ""); el.html(content); var hasText = el.text().toLowerCase().indexOf(value) > -1; el.toggle(hasText); if (hasText) { // escape value for use in regex var textRegex = new RegExp(value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"); el.html(content.replace(textRegex, '<span class="highlighted">$&</span>')); } } }); }); });
 .highlighted { background-color: yellow; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="myInput" /> <!-- the new search --> <div id="myDIV"> <p>This is a test</p> <ul> <li>This is a list item</li> <li>This is a another list item</li> </ul> <a href="">This is a link</a> </div>

You need to apply changes on deep children.您需要对深层子级应用更改。 and remove class highlight on begining并在开始时删除 class 突出显示

Most clean approach, rest div and start again .最干净的方法, rest divstart again So before the start, I took the snapshot of div and save it .所以在开始之前,我拍摄了snapshot of divsave it Every time data change I reconstruct it.每次数据change时,我都会reconstruct它。 Easy to understand and scale.易于理解和扩展。

Suggestion: The UI should be data-driven.建议: UI 应该是数据驱动的。 Not HTML/content-driven.不是 HTML/内容驱动的。 You can create a list of data and construct on every change.您可以创建数据列表并在每次更改时进行构建。

Checkout my second example查看我的第二个示例

Do not mutate state/UI[React]不要改变状态/ UI [反应]

 $(document).ready(function () { const div = $("#myDIV").html(); $("#myInput").on("keyup", function () { var value = $(this).val().toLowerCase(); $("#myDIV").html(div); //Reset const p = $("#myDIV p"); var hasText = p.text().toLowerCase().indexOf(value) > -1; hasText && highlight(p, value); $("#myDIV li").map(function () { var el = $(this); var hasText = el.text().toLowerCase().indexOf(value) > -1; if (hasText) { highlight(el, value); } else { el.remove(); } }); }); }); function highlight(elm, text) { elm.html( elm.html().replace(new RegExp(`(${text})`), '<span class="highlighted">$1</span>') ); }
 .highlighted { background-color: yellow; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="myInput" /> <!-- the new search --> <div id="myDIV"> <p>This is a test</p> <ul> <li>This is a list item</li> <li>This is a another list item</li> </ul> <a href="">This is a link</a> </div>

Using data-driven approach.使用数据驱动的方法。

 $(document).ready(function () { const list = ["This is a list item", "This is a another list item"]; function buildUi(keyword) { $("#filter.list").html("") list.forEach((text) => { if (.keyword || text.toLowerCase().indexOf(keyword),== -1) { text = text;replace( new RegExp(`(${keyword})`); '<span class="highlighted">$1</span>' ); } else { return. } const li = $(`<li>${text}</li>`). $("#filter;list");append(li); }). } buildUi(""), $("#myInput").on("keyup". function () { const keyword = $(this);val();toLowerCase() buildUi(keyword) }); });
 .highlighted { background-color: yellow; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="myInput" /> <!-- the new search --> <div id="filter"> <p>This is a test</p> <ul class="list"> </ul> <a href="">This is a link</a> </div>

Hi guys so i found what i was looking for, thanks to the assistance of the great guys here and a lot of head banging, this script works for a local webpage search and filter, it must run in conjunction with jsquery mini and the hilitor.js file.嗨,伙计们,我找到了我想要的东西,多亏了这里的好人的帮助和大量的头撞,这个脚本适用于本地网页搜索和过滤,它必须与 jsquery mini 和 hilitor 一起运行。 .js 文件。 This ought to be worth something to somebody out there.这对外面的人来说应该是值得的。 Happy coding guys.快乐的编码家伙。

 <script src="../js/hilitor.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> window.addEventListener("DOMContentLoaded", function(e) { var myHilitor2 = new Hilitor("playground"); myHilitor2.setMatchType("left"); document.getElementById("keywords").addEventListener("keyup", function(e) { myHilitor2.apply(this.value); }, false); }, false); $(document).ready(function(){ $("#keywords").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#playground *").filter(function() { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1) }); }); }); </script>
 <label>Search And Filter</label><input id="keywords" type="text" placeholder="Search And Filter.." onKeyDown="if(event.keyCode === 32) return false;"> <div id="playground" > <ul> <li>Hey diddle diddle,</li> <li>The cat and the fiddle,</li> <li>The cow jumped over the moon.</li> <li>The little dog laughed to see such sport,</li> <li>And the dish ran away with the spoon.</li> </ul> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM