简体   繁体   English

JQuery将textarea值复制到div中进行打印

[英]JQuery copy textarea value into a div for printing

I'm trying to set up print styles for textareas on a very long form. 我正在尝试以非常长的形式为textareas设置打印样式。 The client needs to be able to print the page (from any browser) with all of the text shown from each textarea. 客户端需要能够使用每个文本区域显示的所有文本打印页面(从任何浏览器)。 I haven't found any way to do this with pure CSS (I've tried overflow:visible and height:auto/100% attributes and neither work) so I am wondering how I can accomplish this with jquery. 我还没有找到任何方法用纯CSS做这个(我已经尝试过溢出:可见和高度:自动/ 100%属性并且都不起作用)所以我想知道如何用jquery完成这个。

FYI, there will be no on click function or anything. 仅供参考,没有点击功能或任何东西。 I have the textarea displaying for screen and a hidden div below it. 我有textarea显示屏幕和下面隐藏的div。 For print, I am hiding the textarea and showing the "forprint" div. 对于打印,我隐藏了textarea并显示“forprint”div。 So I need the value for the printed div to always match the value of the textarea. 所以我需要打印div的值始终匹配textarea的值。

<div class="forscreen">@Html.TextAreaFor(a => a.Subtitle, AdminOnlyAttribute(new Dictionary<string, object>() { { "class", "k-textbox" }, { "rows", "6" } }))</div>
<div class="forprint"></div>


.forprint {
    display: none;
}

@media print {
    .forprint {
        display: block;
        height: auto;
    }

    .forscreen {
        display: none;
    }
}

Any help would be much appreciated! 任何帮助将非常感激!

EDIT: Naturally, I figured it out shortly after I posted the question. 编辑:当然,我在发布问题后不久就明白了。 Here is the jquery that did it: 这是执行它的jquery:

$('div textarea').focus(function () {
        var copyText = $(this).val();
        $(".forprint").html(copyText);
    });

Now, I am wondering is there a way that I can do this for all textareas on the page without creating unique ID's and functions for each of them? 现在,我想知道有没有办法可以为页面上的所有textareas执行此操作,而无需为每个文本创建唯一的ID和函数?

I struggle to get what you're really doing but to get the text from all textareas once a user clicks on the print button, based on my little knowledge, the window.document object loses focus - blur event. 我很难得到你真正在做的事情,但是一旦用户点击打印按钮就能从所有textareas获取文本,基于我的小知识,window.document对象失去焦点 - 模糊事件。 With focus on that event to trigger the text copying, I think the following would do: 关注该事件以触发文本复制,我认为以下内容会:

$(document).blur(function(e)
{
   var textObj = $("textarea").toArray();
   for(var i = 0; i<textObj.length; ++i)
   {
       var mytext = textObj[i].val();
       $(".forprint").append(mytext);
   }
}

You may also want to try with the blur event of the body element. 您可能还想尝试使用body元素的blur事件。 I hope that works. 我希望有效。 Regards and all the best 关心并且一切顺利

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

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