简体   繁体   English

想要使用JavaScript打印页面的特定部分

[英]Want to print a particular section of a page using JavaScript

I'm basing my code off of this solution by Ben Nadel 我基于Ben Nadel的解决方案编写代码

Here is the code included in the page 这是页面中包含的代码

<script type="text/javascript">
    // When the document is ready, initialize the link so
    // that when it is clicked, the printable area of the
    // page will print.
    $(function() {
        // Hook up the print link.
        $("a").attr("href", "javascript:void(0)").click(function() {
            // Print the DIV.
            $( ".printable" ).print();

            // Cancel click event.
            return(false);
        });
    });
</script>

Here is a codepen 这是一个codepen

The Buttons are not working in the pen but do work locally. 按钮在笔中不起作用,但在本地起作用。

What i'm trying to do is have the user print the contents of each list item which as you can tell is a Q&A so essentially have the user be able to print each Q&A pair when they click the button. 我想做的是让用户打印每个列表项的内容,正如您所知道的,这是一个问答,因此从本质上讲,用户在单击按钮时可以打印每个问答对。

I've only included two to provide the minimal example to help me figure out where my error is. 我仅提供了两个示例,以提供最小的示例来帮助我确定错误所在。

What's happening is that no matter which button I click it will always print the first "li" with the class of "printable" and i'm not sure how to distinguish each section so that the button understands to only print 'this' and not the first li that has that class which is what it's doing. 发生的事情是,无论我单击哪个按钮,它都将始终打印带有“可打印”类的第一个“ li”,并且我不确定如何区分每个部分,以便该按钮理解为仅打印“ this”而不是第一个具有该类的li就是它在做什么。

Obviously this is a problem since each Answer will have a "click to print" button and I don't want them to all print the same Q&A pair. 显然,这是一个问题,因为每个答案都有一个“单击以打印”按钮,我不希望他们都打印相同的问答集。

Does this make sense? 这有意义吗?

My instinct is to have some kind of loop in play or iterate through an array, but i'm very new to JavaScript so i'm looking for a moderately challenging solution. 我的本能是在播放某种循环或遍历数组,但是我对JavaScript非常陌生,所以我正在寻找一个中等难度的解决方案。

You can use simple java script to print specific div of a page 您可以使用简单的Java脚本来打印页面的特定div

    var prtContent = document.getElementById("your div id");
    var WinPrint = window.open('','','letf=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
    WinPrint.document.write(prtContent.innerHTML);
    WinPrint.document.close();
    WinPrint.focus();
    WinPrint.print();
    WinPrint.close();

Yes, it does make sens. 是的,确实有道理。 You are selecting elements to print with $('.printable').print([1]); 您正在选择要使用$('。printable')。print([1]);打印的元素。 It means, print the first element that match my selection, that is .printable. 这意味着,打印与我的选择匹配的第一个元素,即.printable。 The first element that has the class printable. 具有可打印类的第一个元素。 What you should do is bind each button with the appropriate section and use IDs instead of classes to select elements. 您应该做的是将每个按钮与适当的部分绑定在一起,并使用ID而不是类来选择元素。 As IDs are unique ! 由于ID是唯一的!

function(){
        $('.printBtn').attr('href', 'javascript:void( 0 )').click(function(){
            //select the corresponding section, first parent li that have .printable class
            var section = $(this).closest("li.printable");
            // Print Div
            $(section).print([1]);

            // Cancel click event
            return(false);
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择页面上的特定部分不包括所有使用javascript的部分? - Select particular section on my page not include all section using javascript? 我想使用 Javascript 在引导导航栏中突出显示当前部分和当前页面 - I want to Highlight the current section and current page in the bootstrap navbar using Javascript 我要打印页面时javascript中断 - javascript breaks when I want to print a page 窗口的Javascript打印部分 - Javascript print section of window 如何使用 javascript 执行器在 selenium 中的特定网页部分向下滚动 - How to scroll down in a particular section of webpage in selenium using javascript executor 如何使用javascript导航到页面的另一部分? - how to navigate to another section of the page using javascript? 想要重定向到特定部分 id 的下一页而不在 url 中显示 #id - want to redirect to next page on particular section id without showing #id in the url 使用jsf和javascript将页面滚动到特定面板 - scrolling page to a particular panel using jsf and javascript 部分页面使用Java验证程序标注控制不完整 - Validator Callout Control Using Javascript for section of page not full page 如何使用 Javascript 方法移动到另一个页面,然后移动到页面的某个部分 - How to move to another page and then to a certain section of the page using a Javascript method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM