开发者

JS - print only <div> from another file

开发者 https://www.devze.com 2023-03-07 11:00 出处:网络
Now I\'m using onClick=\"window.print(); return false;\"... But I nee开发者_如何转开发d to print a div content form another file.

Now I'm using onClick="window.print(); return false;"...

But I nee开发者_如何转开发d to print a div content form another file.

Example: I have the file buttons.html, but in this file is my print button with window.print(). I need when this button is pressed to print only div content (e.g. div id=...), but from the file index.html.

I hope you understand me.

*Please give more detailed code, I'm new.

Thanks!


function printDivs()
{
    var divs = document.getElementsByTagName("div");
    var text = "";

    for( var i=0; i<divs.length; i++ )
    {
        var div = divs[i];

        if (typeof(div.nodeName) !== "undefined")
        {
            text = text + div.innerHTML + "<br /><br />";
        }
    }

    document.body.innerHTML = text;

    window.print();
}

That should work, as long if its executed on the same page. Is this what you mean or do you mean something else?

You can call this script by using: Print for example.


That's unusual, but certainly possible:

// buttons.html, Using jQuery
$('button').click(function() {
  $.get('index.html', function(h) {
    $('body').html($('div#content', h).html());
    window.print();
  }, "html");
});
0

精彩评论

暂无评论...
验证码 换一张
取 消