Originalmente inviato da
darbula
Codice:
jQuery(document).ready(function(){
var search = '<?php echo get_search_query(); ?>';
var fromsearch = '<?php
$outputdarbula = '';
if(isset($_GET['fromsearch']) && is_string($_GET['fromsearch'])) {
$outputdarbula = $_GET['fromsearch'];
}
echo $outputdarbula; ?>';
if(search !== null && search !== ''){
jQuery('body').highlight(search,'highlight');
jQuery('#main article a').each(function(i,e){
var newUrl = jQuery(e).prop('href') + '?fromsearch=' + encodeURIComponent(search);
this.href = newUrl;
});
}
if(fromsearch !== ''){
jQuery('body').highlight(fromsearch,'highlight');
}
});
Funziona, però ho notato che se ci sono più parole in un paragrafo me ne trova solo una e non tutte in quel paragrafo.
Se invece ho una sola parola in ogni paragrafo le trova tutte.
Codice HTML:
<script>
jQuery.fn.highlight = function(what, spanClass) {
return this.each(function() {
var container = this,
content = container.innerHTML,
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)', 'ig'),
replaceWith = '$1<span ' + (spanClass ? 'class="' + spanClass + '"' : '') + '">$2</span>$3',
highlighted = content.replace(pattern, replaceWith);
container.innerHTML = highlighted;
});
}
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
jQuery(document).ready(function(){
var search = '<?php echo get_search_query(); ?>';
var fromsearch = '<?php $output = '';
if(isset($_GET['fromsearch']) && is_string($_GET['fromsearch'])) {
$output = $_GET['fromsearch'];
}
echo $output; ?>';
if(search !== null && search !== ''){
jQuery('body').highlight(search,'highlight');
jQuery('#main article a').each(function(i,e){
var newUrl = jQuery(e).prop('href') + '?fromsearch=' + encodeURIComponent(search);
this.href = newUrl;
});
}
if(fromsearch !== null && fromsearch !== ''){
jQuery('body').highlight(fromsearch,'highlight');
}
});
</script>