<input id="spterm" type="text" name="spterm" placeholder=" ?"><br /> <div id="spresult"> </div>
span.highlight { background-color: #C6D9DB; cursor: pointer; } span.splink { color: #0A5794; cursor: pointer; }
var minlen = 3; // var paddingtop = 30; // var scrollspeed = 200; // var keyint = 1000; //
function dosearch() { term = jQuery('#spterm').val(); jQuery('span.highlight').each(function(){ // jQuery(this).after(jQuery(this).html()).remove(); }); var t = ''; jQuery('div.entry-content').each(function(){ // jQuery(this).html(jQuery(this).html().replace(new RegExp(term, 'ig'), '<span class="highlight">$&</span>')); // n = jQuery('span.highlight').length; // console.log('n = '+n); if (n==0) jQuery('#spresult').html(' '); else jQuery('#spresult').html(': '+n); }); } jQuery('#spterm').keyup(function(){ if (jQuery('#spterm').val()!=term) // , if (jQuery('#spterm').val().length>=minlen) { // dosearch(); // , } else jQuery('#spresult').html(' '); // , DIV });
if (n==0) jQuery('#spresult').html(' '); else jQuery('#spresult').html(': '+n); if (n>1) // , { var i = 0; jQuery('span.highlight').each(function(i){ jQuery(this).attr('n', i++); // , }); jQuery('span.highlight').not(':last').attr({title: ', '}).click(function(){ // , , jQuery('body,html').animate({scrollTop: jQuery('span.highlight:gt('+jQuery(this).attr('n')+'):first').offset().top-paddingtop}, scrollspeed); // }); jQuery('span.highlight:last').attr({title: ', '}).click(function(){ jQuery('body,html').animate({scrollTop: jQuery('#spterm').offset().top-paddingtop}, scrollspeed); // }); }
jQuery('#spterm').keyup(function(){ var d1 = new Date(); time_keyup = d1.getTime(); if (jQuery('#spterm').val()!=term) // , if (jQuery('#spterm').val().length>=minlen) { // setTimeout(function(){ // var d2 = new Date(); time_search = d2.getTime(); if (time_search-time_keyup>=keyint) // dosearch(); // , }, keyint); } else jQuery('#spresult').html(' '); // , DIV });
if (window.location.hash!="") // { var t = window.location.hash.substr(1, 50); // jQuery('#spterm').val(t).keyup(); // jQuery('#spgo').click(); // }
jQuery(document).ready(function(){ var minlen = 3; // var paddingtop = 30; // var scrollspeed = 200; // var keyint = 1000; // var term = ''; var n = 0; var time_keyup = 0; var time_search = 0; jQuery('body').delegate('#spgo', 'click', function(){ jQuery('body,html').animate({scrollTop: jQuery('span.highlight:first').offset().top-paddingtop}, scrollspeed); // }); function dosearch() { term = jQuery('#spterm').val(); jQuery('span.highlight').each(function(){ // jQuery(this).after(jQuery(this).html()).remove(); }); var t = ''; jQuery('div#content').each(function(){ // jQuery(this).html(jQuery(this).html().replace(new RegExp(term, 'ig'), '<span class="highlight">$&</span>')); // n = jQuery('span.highlight').length; // console.log('n = '+n); if (n==0) jQuery('#spresult').html(' '); else jQuery('#spresult').html(': '+n+'. <span class="splink" id="spgo"></span>'); if (n>1) // , { var i = 0; jQuery('span.highlight').each(function(i){ jQuery(this).attr('n', i++); // , }); jQuery('span.highlight').not(':last').attr({title: ', '}).click(function(){ // , , jQuery('body,html').animate({scrollTop: jQuery('span.highlight:gt('+jQuery(this).attr('n')+'):first').offset().top-paddingtop}, scrollspeed); // }); jQuery('span.highlight:last').attr({title: ', '}).click(function(){ jQuery('body,html').animate({scrollTop: jQuery('#spterm').offset().top-paddingtop}, scrollspeed); // }); } }); } jQuery('#spterm').keyup(function(){ var d1 = new Date(); time_keyup = d1.getTime(); if (jQuery('#spterm').val()!=term) // , if (jQuery('#spterm').val().length>=minlen) { // setTimeout(function(){ // var d2 = new Date(); time_search = d2.getTime(); if (time_search-time_keyup>=keyint) // dosearch(); // , }, keyint); } else jQuery('#spresult').html(' '); // , DIV }); if (window.location.hash!="") // { var t = window.location.hash.substr(1, 50); // jQuery('#spterm').val(t).keyup(); // jQuery('#spgo').click(); // } });
Source: https://habr.com/ru/post/257025/
All Articles