Javascript find text on page

前端 未结 5 606
广开言路
广开言路 2021-01-14 22:13

I need to run a search and replace on HTML similar to the following... I need to have \"Find Next\" \"Replace\" and \"Replace All\" options.... the trick is that I need to r

5条回答
  •  长情又很酷
    2021-01-14 22:40

    I would build a data object while traversing matched elements. Then send the object so you do not have multiple ajax request from a loop. jsfiddle:

    This is yet another test
    This is test data

    Script:

    var searchTerm = 'This is',
        replaceWith = 'This is new',
        updateObj = {};
    $("#sheet div.item:contains('" + searchTerm + "')").each(function(){
       // use id to build an update object
        updateObj[this.id.replace('field-', '')] = {
            oldText: searchTerm, 
            newText: replaceWith
        }; // not sure what you are trying to save here
       // manipulate html
       this.innerHTML = this.innerHTML.replace(searchTerm, replaceWith);
    });
    // after, send ajax data `updateObj`
    

提交回复
热议问题