In jQuery è semplice evidenziare la prima parola di un testo.
La soluzione è la seguente:
(function( $ ) {
$.fn.firstWord = function( options ) {
options = $.extend({
className: "first-word"
}, options);
var Worder = function( element ) {
this.element = element;
this.init();
};
Worder.prototype = {
init: function() {
this.create();
},
create: function() {
var currentHTML = this.element.html(),
words = currentHTML.split( " " ),
firstWord = words[0],
span = "<span class='" + options.className + "'>" + firstWord + "</span>";
this.element.html( currentHTML.replace( firstWord, span ) );
}
};
return this.each(function() {
var $element = $( this );
if( !$element.data( "worder" ) ) {
var wordr = new Worder( $element );
$element.data( "worder", wordr );
}
});
};
})( jQuery );