简体   繁体   English

如何在javascript中的IE9中打印文本框的内容

[英]How to print content of textbox in IE9 in javascript

I have to print a text box with its value using JavaScript when user enter the value at run time but the below code is not compatible with IE9+ textbox is within div tag 当用户在运行时输入值时,我必须使用JavaScript打印一个带有其值的文本框,但以下代码与IE9 +文本框不兼容,该文本框位于div标记内

Print:function()
{
     var divtoprint = document.getElementById('div1');

     var popupWin = window.open('', '_blank', 'width=500,height=400');
     popupWin.document.open();
     popupWin.document.write('<html><body onload="window.print()">' + divtoprint.innerHTML + '</html>');
     popupWin.document.close();
 }

Please can you help? 请你帮忙

I have changed : 我变了 :

<!DOCTYPE html>
<html>
<head>
<script>
function print(){
     var divtoprint = document.getElementById('main-div').innerHTML;
     alert(divtoprint);
     var popupWin = window.open('', '_blank', 'width=500,height=400');
     popupWin.document.open();
     popupWin.document.write('<html><body onload="window.print()">' + divtoprint + '</html>');
     popupWin.document.close();
 }
</script>
</head>
<body>
 <div>
 <div id="main-div">
    <input type="text" name="FirstName" value="Mickey">
    </div>
    <input type="button" value=" Print Terms " onClick="print()">
</div>

</body>
</html>

and it works f9 in ie9,ie10. 它在ie9,ie10中可以使用f9。

here is the screen shot in my ie 10: 这是我ie 10中的屏幕截图:

在此处输入图片说明

and this: 和这个: 在此处输入图片说明

IE has some strict validation of html. IE对html进行了一些严格的验证。 Chrome, firefox and the other browsers are not so strict. Chrome,firefox和其他浏览器并不严格。

In your code you are missing the closing body tag. 在您的代码中,您缺少结束标记。 You will need to this tag and i sure your code will work in IE. 您将需要此标记,并且我确定您的代码将在IE中运行。

Replace: 更换:

popupWin.document.write('<html><body onload="window.print()">' + divtoprint.innerHTML + '</html>');

By: 通过:

popupWin.document.write('<html><body onload="window.print()">' + divtoprint.innerHTML + '</body></html>');

At the end of the line I added the closing tag. 在该行的末尾,我添加了结束标记。

I really doubt this can be done to be honest. 我真的怀疑这是否可以做到诚实。 Besides, a common practice for printing webpages is attaching a stylesheet for printing, in which you are hiding all elements but the ones you want to print. 此外,打印网页的一种常见做法是附加样式表以进行打印,其中隐藏了所有要打印的元素,但隐藏了所有元素。
Here is a nice blog article about that: 这是一篇不错的博客文章:
print stylesheets 打印样式表

Here is a quick example: 这是一个简单的示例:

<!DOCTYPE html>
<html>
<head>
    <style media="print">
        .noprint { display: none; }
    </style>
</head>
<body">
    <div>
        <div id="main-div">
            <input type="text" id="mytextbox" name="FirstName" value="Mickey"/>
        </div>
        <input type="button" class="noprint" value=" Print Terms " onClick="window.print()">
    </div>
</body>

Now if your user goes to "print preview" in their browser, they won't see the "print" button (and the same way the page will be printed when they click "print" button on the page) 现在,如果您的用户在浏览器中转到“打印预览”,他们将不会看到“打印”按钮(与单击页面上的“打印”按钮时打印页面的方式相同)

I hope that will help you. 希望对您有所帮助。

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

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