简体   繁体   English

Javascript“打印div的内容”功能重复相同的内容

[英]Javascript “print contents of div” function repeating the same content

I am attempting to edit a Wordpress plugin for a client which is used to print a coupon by using a custom javascript function to print the contents of a div. 我正在尝试为客户端编辑Wordpress插件,该插件用于通过使用自定义javascript函数来打印div的内容来打印优惠券。 This plugin is called by a shortcode: 该插件通过短代码调用:

[print-button target='div#foo']

The target is the css ID of a container div with a jpg of a coupon being the only content. 目标是容器div的css ID,其中优惠券的jpg是唯一的内容。 When you click on the generated "print" button then a window opens and brings up a print dialogue for the jpg. 当您单击生成的“打印”按钮时,将打开一个窗口,并弹出jpg的打印对话框。 With one instance of this plugin on a page it works perfectly, however when you have two or more instances then the only jpg that opens in the new print window is the last one on the page. 在页面上使用此插件的一个实例时,它可以完美工作,但是,当您有两个或更多实例时,在新的打印窗口中打开的唯一jpg是页面上的最后一个jpg。 Here is the plugin code: 这是插件代码:

add_shortcode("print-button", "sc_show_print_button");

function sc_show_print_button($atts, $content = null){

    $target_element = $atts['target'];

    if($target_element == ''){$target_element = "document.body";}

    $output = "<input id=\"coupon\" type='button' onClick='return pop_print()' value='Print Coupon'/>


    <script type='text/javascript'>
    function pop_print(){
        w=window.open(null, 'Print_Page', 'scrollbars=yes');
        w.document.write(jQuery('$target_element').html());
        w.document.close();
        w.print();
    }
    </script>";


    return  $output;
}

I have echo'd out the $target_element variable at the end of the function and it does list each 'target' variable correctly. 我在函数末尾回显了$ target_element变量,它确实正确列出了每个“ target”变量。 I just cannot figure out why only the last instance's image is showing up when you click the "print" button for every other instance. 我只是不知道为什么当您单击其他每个实例的“打印”按钮时,仅显示最后一个实例的图像。 I'm assuming that it's something simple in the javascript that I'm not seeing. 我假设这是我没有看到的javascript中的简单内容。

I am not the original author of this plugin, and contacting them is not an option. 我不是该插件的原始作者,与他们联系不是一种选择。

Thank you for your help. 谢谢您的帮助。

Because, when you have more than one instances of "buttons", you have several definition of function "pop_print" with static code. 因为,当您有多个“按钮”实例时,您将使用静态代码对函数“ pop_print”进行几种定义。 And, of course, the last function always executes. 而且,当然,最后一个函数总是执行。

Try to send target element as argument of function "pop_print". 尝试将目标元素作为函数“ pop_print”的参数发送。

Example: 例:

add_shortcode("print-button", "sc_show_print_button");

function sc_show_print_button($atts, $content = null){

    $target_element = $atts['target'];

    if($target_element == ''){$target_element = "document.body";}

    $output = "<input id=\"coupon\" type='button' onClick='return pop_print(\'$target_element\')' value='Print Coupon'/>


    <script type='text/javascript'>
    function pop_print(target_element){
        w=window.open(null, 'Print_Page', 'scrollbars=yes');
        w.document.write(jQuery(target_element).html());
        w.document.close();
        w.print();
    }
    </script>";


    return  $output;
}

And, of course, try to replace function from plug-in into external js file. 并且,当然,尝试将插件中的功能替换为外部js文件。

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

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