dimanche 25 janvier 2015

how to select random keyword?

i have one problem with this code. it selects the first three keyword. what if i want to select the keyword in random number? currently, it has 7 keywords and it selects the first three. what if i want to select keyword 3,6,7. how can i do the random function here? thanks in advanced!



(function($) {
$.fn.replacetext = function (target, replacement, max) {
var limit = max || -1;


var $textNodes = this
.find("*")
.andSelf()
.contents()
.filter(function () {
return this.nodeType === 3 && !$(this).parent("a").length;
});

$textNodes.each(function (index, element) {
var $element = $(element);
var words = $element.text().split(/\b/);
var matches = 0;
var text = words.map(function (word, index) {
if (matches >= limit) {
return word;
}

if (word.match(target)) {
++matches;
return word.replace(target, replacement);
}

return word;
});

$element.replaceWith(text.join(''));
});
};
})(jQuery);


$("p").replacetext(/\bdress\b/gi, "<a href='http://www.google.com'>$&</a>", 3);




Aucun commentaire:

Enregistrer un commentaire