HTML :
<p data-schedule="end">meeting visio.</p>
<p data-schedule="to-do"> call product.</p>
<p data-schedule="end">visio conf.</p>
<style>
.finded{
color : red;
}
</style>
Js :
function talksAbout(node, string) {
if (node.nodeType == document.ELEMENT_NODE) {
for (var i = 0; i < node.childNodes.length; i++) {
if( talksAbout(node.childNodes[i], string) ){
return true;
}
}
return false;
} else if (node.nodeType == document.TEXT_NODE) {
if( node.nodeValue.indexOf(string) > -1 ){
node.parentNode.classList.add("finded");
return true;
}
return false;
}
}
console.log(talksAbout(document.body, "visi"));
Afficher le code !