Clone HTML and remove some nodes using jQuery

I am developing a small tool for live editing using Chrome DevTools, and I have a small “Save” button that captures HTML and sends it to the server to update the static file (.html) using Ajax. Very simple.

My problem is that I need to filter the HTML code before sending it to the server, I need to delete some nodes, and I'm trying to achieve this using jQuery, something like this:

// I grab all the HTML code
var html = $('<div>').append($('html').clone()).html();
// Now I need to remove some nodes using jQuery
$(html).find('#some-node').remove();
// Send the filtered HTML to server
$.post('url/to/server/blahblahblah');

I already tried this. Using jQuery to search an HTML string without success. I cannot use jQuery for my cloned HTML code.

Any idea on how to do this?

+4
source share
2

DOM HTML. jQuery DOM, .

,

  • (, HTML),

  • div -

  • div HTML

  • HTML DOM $(html) ( )

  • , html.

, html , DOM, .


, , , , .

, , , , .clone(), .find().remove(), .html()

var result = $("html").clone(false);

result.find("#some-node").remove();

var html = result.html();
+5

, ?

var html = $('html').clone();
html.find('#some-node').remove();
0

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


All Articles