问题描述:

I have a page with lots of stuffs. With a script I change the content et I have content1 for button1, content2 for button2. If a click on button1 the content2 disappear and the content1 appear.

I want to get the html code of the page displayed in the browser, not these of the content1.

Have you an idea ? Thanks :)

EDIT: I want the html code of the page displayed in the browser not in the page .html so .html() doesn't help me.

For Example: How can i do to get the content of the page : http://stackoverflow.com/ in a variable ? If possible with the balise html.

(ex: ... < h1 > Top Questions < /h1 > ... all in a variable ) I talk about the page displayed not the source code.

In my page I have differents div but I don't want to get all the html, I just want to get these displayed in the browser. If I have <html> <body> <div id="content1"> blabla </div> <div id="content2"> blabla </div> </body> </html> and with a script I display just content2. In the browser, just the content2 is displayed and I want to get <html> <body> <div id="content2"> blabla </div> </body> </html>

EDIT: my code :

<div class="col-md-3">

<ul class="nav nav-list">

<li>

<a href="#section1">Section1</a>

<ul class="nav nav-list">

<li ng-show="button1">

<a href="#section1_1">Section1_1</a>

</li>

<li ng-show="!button1">

<a href="#section1_2">Section1_2</a>

</li>

</ul>

</li>

</ul>

</div>

With a button I put button1 to true and an other to false. So the page is different in function of this.

I want to get the html code when the button is to true or false. So whether :

<div class="col-md-3">

<ul class="nav nav-list">

<li>

<a href="#section1">Section1</a>

<li ng-show="button1">

<a href="#section1_1">Section1_1</a>

</li>

</li>

</ul>

</div>

or :

<div class="col-md-3">

<ul class="nav nav-list">

<li>

<a href="#section1">Section1</a>

<li ng-show="!button1">

<a href="#section1_2">Section1_2</a>

</li>

</li>

</ul>

</div>

In fact the result which is displayed.

网友答案:

$('body').html(); will return you the content of body.

$('body').html(var VARIABLE); will change current content of body by VARIABLE

I guess you can do what you want with it!

EDIT :

To hide the div with ID=content1 :

$('#content1').hide(); 

To show it :

$('#content1').show();

To get code in a variable :

var html = $('#content1').html();

网友答案:

I finally found the answer.

    var $node = $('#myContainer'); // jQuery object of node to be cloned
    $node.find(':hidden').addClass('markedForRemoval');

    var $clone = $node.clone();

    $clone.find('.markedForRemoval').remove();

    // tidy up:
    $clone.find('.markedForRemoval').removeClass('markedForRemoval');
    $node.find('.markedForRemoval').removeClass('markedForRemoval');

    var codeHtml = $clone.html();

This is it :)

相关阅读:
Top