JQuery'deki her kelimeyi yazmaya çalışıyorum ve renkli bir arka plan veriyorum, bir çözüm buldum, ancak yalnızca html içeriği için çalışıyor, girişi renklendirmek istiyorum arama çubuğu için. Ve ne için bu bir examble ben jQuery('#search')
için jQuery('*')
değiştirmeye çalıştı ama işe yaramadıArama çubuğundaki her sözcüğün arka plan rengini değiştirin
HTML
some content
<div>another content
<input id="search" type="text" value="dsfdsfdsf sdf dsfsdfsfs">
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ac turpis
</p>
content in body
jQuery
//the main point here is you need to replace only the contents of the text node, not all the html parts
var colours = ['red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'gray'];
var lastC;
jQuery('*').contents().each(function() {
//not a text node or there is no content to replace
if (this.nodeType != 3 || !this.nodeValue.trim()) {
return;
}
//replace the value of the text node
$(this).replaceWith(this.nodeValue.replace(/\w+/g, function (part) {
var c = colours[Math.floor(Math.random() * colours.length)];
while (c == lastC) {
var c = colours[Math.floor(Math.random() * colours.length)];
}
lastC = c;
return '<span style="background-color: '+c+';">' + part.split("").join("") + '</span>'
}))
});
bulundu.
Canlı örnek https://jsfiddle.net/7hs9mgLp/2/
Ne düzeltmek yapmam gerekir? Bu işlev bir yayılma elemanda englobe o kadar içeriği almak ve elemanın değil değeri çünkü Aslında
Bana çözümü hakkında bir fikir verdi, teşekkür ederim, ama ben arama çubuğuna her kelimeyi istediğiniz renklendirilecek tüm sayfayı istemiyoruz Farklı bir arka plan rengi, yaptığınız gibi tüm arka plan değil. Teşekkür ederiz –
Bunun gibi bir http://s9.postimg.org/t6qp02vin/dsfsfdsfdsfdsf.png –
Bunu yapmak biraz zor ama istediğin sonucu elde etmenin bir yolunu buluyorum. Cevabımı güncelledim. –