Run jQuery on a variable, not a document

Did you know whether it is possible to run jQuery for a variable containing html, and not just for the document itself.

Example:

bg.inspectorGetRawHtml = function(){
    var c = jQuery("#bg-admin-inspect-wrapper").html();
    jQuery(".bg-admin-inspect-state", c).remove();
    console.debug(c);
}

So basically reading a segment of html pages into a variable and then doing jQuery manipulation on that line.

If I console.debug, then the actual jQuery is deleted, it seems like a match, but var is not processed. I know jQuery relies on the DOM, and maybe that’s why it doesn’t work, but if anyone has an understanding of this. Edit After the lonesomeday suggestion, here is the code I got into:

bg.inspectorGetRawHtml = function(){
    var c = jQuery("#bg-admin-inspect-wrapper").clone();
    jQuery(".bg-admin-inspect-state", c).remove(); //The ,c specifies what element to work on.
    console.debug(c.html());
}
+3
source share
1 answer

You need the $ () function. detach:

bg.inspectorGetRawHtml = function(){
    var c = jQuery("#bg-admin-inspect-wrapper").detach();
    console.debug(c);
}

DOM jQuery.


. HTML jQuery- . ,

$(document).ready(function() {
    var newText = '<div><p>Some text here</p><img src="image.png" /></div',
        $newDiv = $(newText);

    $newDiv.addClass('someClass');

    $('body').html($newDiv);
});

div , "someClass" div div.

+4

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


All Articles