Why does this jQuery not load / parse my HTML string?

jQuery.get(window.location.href, function(data) { alert(data); alert($(data).html()); }); 

The first popup is all the good and healthy HTML.

The second popup is empty. What for? (HTML corresponds to XHTML)

0
jquery html parsing get
source share
4 answers

From the documentation :

An HTML string cannot contain elements that are not valid in a div, such as html, head, body, or header elements.

If you are extracting the full HTML document, you will have many elements that may not appear in the div.

+3
source share

Because it will return a string with all the HTML. data not a jQuery object.

0
source share

I tried this on my pc. You will receive the following:

 " <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"><title> </title><link href="App_Themes/selectors.css" rel="stylesheet" type="text/css" /></head>................etc 

This will not be parsed in jQuery obejct. You must be on a server side script page that will explicitly output HTML.

I suggest that if you really need an element in HTML, you can remove it from the text using the built-in string methods.

0
source share

Change your code to something like this

 $('#yourContainingDiv').html(data); 

Html data will be placed in div tag

0
source share

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


All Articles