Using jquery to parse the html of a webpage, modify it and then paste it into the webpage again

I'm currently trying to add tags around certain words on a web page. There can be many different identifiers, classes, and names on this web page, so it is important that if I try to set the range around "foo" I do the following:

<p class='foo'>blah blah blah foo blah blah blah </p>

changes to

<p class='foo'>blah blah blah <span class='bar'> foo </span> blah blah blah </p>

but not

<p class='<span class='bar'> foo </span>'>blah blah blah <span class='bar'> foo </span> blah blah blah </p>

I am studying website parsing using the following code:

var obj = $(this.innerHTML);  //in my selector which selects the part of the website I want to apply the span to

I can view this data structure in firebug, but changing it (or searching) is more complicated.

If I look at obj [1], the innerHTML property is as follows:

<h4 id="3423df" class="pzCMD">blah blah foo blah </h4>

I do not know how to change this element (if possible) or add new nodes to it. I can extract the "text" by looking at textContent

obj[1].textContent = blah blah foo blah

but I'm not sure how to change the html code of this element to

<h4 id="3423df" class="pzCMD">blah blah <span class='bar' foo <span> blah </h4>

, .

+3
4

. , , , , . + DOM + .

function replaceText(element, pattern, syn_text) {

for (var childi = 0; childi < element.childNodes.length;childi++) {
    var child= element2.childNodes[childi];
    if (child.nodeType==1 && child.className!=syn_text){ //make sure we don't call function on newly created node
        replaceText(child, pattern, syn_text);  //call function on child
    }
    else if (child.nodeType==3){ //this is a text node, being processing with our regular expression
        var str = child.data;
        str = str.replace(pattern,function(s, p1,p2,p3) {
            var parentNode = child.parentNode;
            do_replace(s, p1,p2,p3,parentNode,pattern,syn_text);
            parentNode.removeChild(child);  //delete old child from parent node.  we've replaced it with new nodes at this point
         });
    }
}}




function do_replace(s, p1,p2,p3,parentNode,pattern,syn_text) {
   if(p1.length>0){   //this might not be necessary
     //create textnode
      var text_node = document.createTextNode(p1);
      parentNode.appendChild(text_node);
   }
   if(p2.length > 0){ //create a span + next_node for the highlighting code
      spanTag = document.createElement("span");
      spanTag.id = "SString" + id++;
      spanTag.className = syn_text;
      spanTag.innerHTML = p2;
      parentNode.appendChild(spanTag);
   }
   if(p3.length > 0){
       //test to see if p3 contains another instance of our string.

      if(pattern.test(p3)){  //if there is a instance of our text string in the third part of the string, call function again
          p3.replace(pattern,function(s, p1,p2,p3) {
            //debugger;
            do_replace(s, p1,p2,p3,parentNode,pattern);
            return;
          });
      }
      else{  //otherwise, it just a plain textnode, so just reinsert it.
          var text_nodep3 = document.createTextNode(p3);
          parentNode.appendChild(text_nodep3);
          return;
      }
    }
    else{ //does this do anything?
        return;
     }
return}

:

syn_highlight = "highlight_me";  //class to signify highlighting 
pattern = new RegExp('([\\w\\W]*?)\\b('+ searchTerm + '[\\w]*)\\b([\\w\\W]*)',"ig");
replaceText($('#BodyContent')[0],pattern,syn_highlight);
0

jQuery.add() dom , Jscript :

 var obj = $(this.innerHTML); 
 obj[1].innerHTML = obj[1].innerHTML+'blah hi</span>';
 //<h4 id="3423df" class="pzCMD">blah blah <span class='bar' foo <span> blah blah hi  </span> </h4>
+1

, , . , : , div foo text foo, span .

<div class="foo">some ttext nmot changed except the foo part</div>

:

<div class="foo">some ttext nmot changed except the <span class="bar">foo</span> part</div>

:

var allFoosInTheWorld = $("div.foo");

for (var i = 0; i < allFoosInTheWorld.length; i++ ){
    var text = $(allFoosInTheWorld[i]).text();
    var index = text.indexOf("foo");

    if ( index != -1 ){
        var elem = '<span class="bar">' + "foo!!!" + '</span>';
        text = text.replace("foo", elem);
        $(allFoosInTheWorld[i]).text(text);
    }
}
0

divs! , , foreach!!! :)

var classesIWant = ["foo", "foo1", "foo2"];
var allDivsInTheWorld = jQuery("div");

for( var i = 0; i < allDivsInTheWorld.length; i++) {
    var element = allDivsInTheWorld[i];
    var good = false;    

    for (var j = 0; j < classesIWant.length; j++){
        if ( $(element).hasClass(classesIWant[j]) )
            good = true;
    }

    if ( !good )
        continue;

    var text = $(element).text();
    var index = text.indexOf("foo");

    if ( index != -1 ){
        var elem = '<span class="bar">' + "foo!!!" + '</span>';
        text = text.replace("foo", elem);
        $(element).text(text);
    }
}
0

Source: https://habr.com/ru/post/1761811/


All Articles