Tag:htmlcssjavascript
Article From:https://segmentfault.com/q/1010000012143218
Question:

I want to achieve the query results highlighted for query keywords, the code is roughly as follows

var html = $(selector).html();
html=html.replace(new RegExp(keyword,’g’),'<font color=”red”>’+keyword+'</font>’);
$(selector).html(html);

The highlighted key was successfully implemented, but the clicking event of the original a tag and the IMG tag failed.
How to solve this problem?
Please do not hesitate to give your advice.

Answer 0:

Which binding does your click event use? On, live? Or else, you can see that jQuery’s event binding can bind events to elements that have not yet appeared in the DOM structure, while others can not.

Answer 1:

You can take a look at the delegate method of jQuery, which can solve your problem.

Answer 2:

$(‘a-certain-selector-parent’).on(‘click’, ‘selector’, function () {

// Binding event
});

Answer 3:

The third floor method is used to delegate the binding of dynamic elements.

Answer 4:

It has been solved, thank you, tree generated HTML elements, I am going to find the corresponding node text, and then replace text. The function has been realized, but I am also aware of this problem, because the plug-in has already bound events during initialization.In my query, the plug-in will be re – initialized, and finally I Replace the HTML of the entire query result, so the corresponding events are destroyed, so that only re bind the event.

Leave a Reply

Your email address will not be published. Required fields are marked *