简体   繁体   中英

Print HTML-page with div that has been clicked on and hide the rest

I have a HTML-page with a lot of different DIVs in it and I want to print out the the DIV that the user has clicked in and hide all the rest.

Can someone tell me how to do this in Javascript please?

<html>
  <head>
    <title>Print Demo</title>

    <script type="text/javascript">
       <!-- MY JAVASCRIPT FUNCITON -->

    </script>

  </head>
  <body>

    <div id="div1">
       <a href="<JSMethod>">Print the page with this div</a>
    </div>

    <div id="div2">
       <a href="<JSMethod>">Print the page with this div</a>
    </div>

    <div id="div3">
       <a href="<JSMethod>">Print the page with this div</a>
    </div>

    </body>
</html>

Hiding an element means setting it's style.display property to "none" . Showing means setting it to "block" for a div element.

In combination with getElementsByTagName , you could accomplish this: http://jsfiddle.net/b9cgM/ .

function show(elem) {
  // hide all divs initially
  var allDivs = document.getElementsByTagName("div");
  for(var i = 0; i < allDivs.length; i++) {
    allDivs[i].style.display = "none";
  }

  // show the appropriate div
  elem.parentNode.style.display = "block";  // parent of the <a> is the <div> to show
}

You could bind the event like <a href="#" onclick="show(this); return false;"> . The element ( this ) is then passed to show .

As a side note, libraries such as jQuery make this even easier; you might want to check that out (though I don't recommend including it if the only use case would be this).

This is just to extend pimvdb's answer .

jQuery:

$("a").on("click", function(){
    $("div").hide();
    $(this).parent().show();
});

Or as suggested:

$("a").on("click", function(){
    $("div").hide();
    $(this).closest("div").show();
});

sorry, there is only window.print() for printing in js, which means you can only print the entire window. if you want some to be able to print your document, make it printable using CSS. for instance, maybe you want your navigation to disappear for printing, but leave the title of your page there and the name of your web site and maybe a page URL (sometimes browsers like firefox cut those off if they are too long). and sometimes some sites take away the browser controls and make the mistake of leaving you with no print button - and it's an online purchasing site... it's happened before.

<style type="text/css">
@media print {
    .boxGreen {
    padding:10px;
    border-color:green;
    border-style:dashed;
    border-width:thin;
    }

}
@media screen {
    .boxGreen {
    padding:10px;
    border-color:green;
    border-style:dashed;
    border-width:thin;
    }
}
</style>

you CAN do an onclick="switchtodiv('someid')" and then after the divs do this:

<div onclick="switchtodiv('span1')">ClickMe<span id="span1">some content</span></div>
<div onclick="switchtodiv('span2')">ClickMe<span id="span2">some content</span></div>
<div onclick="switchtodiv('span3')">ClickMe<span id="span3">some content</span></div>
<!--you can generate these divs using a for statement...-->
<script type="text/javascript">
//switchdiv allows only 1 div tobe 
function switchdiv(id) {
    var ids=new Array('span1','span2','span3');
    var i;
    for (i=0; i < ids.length; i++) {
        if (ids[i] == id) {
            document.getElementById(ids[i]).style.visibility='visible';
            document.getElementById(ids[i]).style.display='block';
        } else {
            document.getElementById(ids[i]).style.visibility='hidden';
            document.getElementById(ids[i]).style.display='none';
        }
    }
}
</script>

You could use a javascript function like document.getElementById(id) to hide the two other divs

So in your function you could just use

function hide1() {
document.getElementById(div2).style.display = "none";
document.getElementById(div3).style.display = "none";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM