简体   繁体   English

jQuery(创建输入后突出显示文本)

[英]jQuery (highlight text after creating input)

I'm very new to jQuery and js. 我对jQuery和js非常陌生。 Here is question. 这是问题。 I need to create a button which adds an input field and then highlights all expressions in given text matching with text in input. 我需要创建一个添加输入字段的按钮,然后突出显示与输入中的文本匹配的给定文本中的所有表达式。 I have no problem with adding new input field. 添加新的输入字段没有问题。 I also found solution for highlighting areas in text. 我还找到了突出显示文本区域的解决方案。 The problem is I can't combine these 2 things in one. 问题是我无法将这两件事合二为一。 So I have following: if there is already input field on page and I type smth here, the matching parts from text are highlighted. 因此,我有以下内容:如果页面上已经有输入字段,并且在此处输入smth,则会突出显示文本中匹配的部分。 But if i use button to add new input field and then type anything there, there is no highlight. 但是,如果我使用按钮添加新的输入字段,然后在其中输入任何内容,则不会突出显示。 Question may be quite silly, so can you at least tell me where exactly I can read about it? 问题可能很愚蠢,所以您至少可以告诉我我在哪里可以确切地了解到它? I'm really new here and have no clue why it's happening. 我真的很新,不知道为什么会这样。 https://jsfiddle.net/0on93vrv/1/ https://jsfiddle.net/0on93vrv/1/

$(document).ready(function() {
//var max_fields      = ; //maximum input boxes allowed
var wrapper         = $(".input_fields_wrap"); //Fields wrapper
var add_button      = $(".add_field_button"); //Add button ID

var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
    e.preventDefault();
    if(x < 2){ //max input box allowed
       x++; //text box increment
    $(wrapper).append('<div><input id="input" type="text" name="mytext[]"/>     </div>');
    //add input box
   }
});
$("input").on("input.highlight", function() {
// Determine specified search term
var searchTerm = $(this).val();
// Highlight search term inside a specific context
$("#context").unmark().mark(searchTerm);
}).trigger("input.highlight").focus();
});

Use (document).on for dynamically created elements like so: (document).on用于动态创建的元素,如下所示:

$( document ).on( "input.highlight", "input", function() {
// Determine specified search term
var searchTerm = $(this).val();
// Highlight search term inside a specific context
$("#context").unmark().mark(searchTerm);
}).trigger("input.highlight").focus();
});

Here is an updated fiddle Fiddle 这是更新的小提琴

The way you add the input.highlight event listener to the input elements input.highlight事件侦听器添加到input元素的方式

$("input").on("input.highlight", function() {
    ....
});

Make it so that the callback function will only be triggered by input.highlight events on input s that are already in the DOM. 使得回调函数将仅由DOM中已经存在的inputinput.highlight事件触发。

Try this if you want to trigger the callback on input s that are dynamically added to the DOM: 如果要触发动态添加到DOM的input的回调,请尝试以下操作:

$("body").on("input.highlight", "input", function() {
    ....
});

you need to have dynamic trigger event.. have a look over.. 您需要具有动态触发事件。

Replace your 更换您的

$("input").on("input.highlight", function() {

with

 $("body").on("input.highlight", "input", function() {

 $(document).ready(function() { //var max_fields = ; //maximum input boxes allowed var wrapper = $(".input_fields_wrap"); //Fields wrapper var add_button = $(".add_field_button"); //Add button ID var x = 1; //initlal text box count $(add_button).click(function(e){ //on add input button click e.preventDefault(); if(x < 2){ //max input box allowed x++; //text box increment $(wrapper).append('<div><input id="input" type="text" name="mytext[]"/></div>'); //add input box } }); $("body").on("input.highlight", "input", function() { // Determine specified search term var searchTerm = $(this).val(); // Highlight search term inside a specific context $("#context").unmark().mark(searchTerm); }).trigger("input.highlight").focus(); }); 
 mark { background: orange; color: black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/mark.js/7.0.0/jquery.mark.min.js"></script> <input type="text" value="test"> <div id="context"> Some text which i want to check for matches </div> <div class="input_fields_wrap"> <button class="add_field_button">Add More Fields</button> <div class="color_input"><input type="text" name="mytext[]"></div> </div> 

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

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