Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 图像返回值“;空”;使用JS_Javascript - Fatal编程技术网

Javascript 图像返回值“;空”;使用JS

Javascript 图像返回值“;空”;使用JS,javascript,Javascript,在我的网页中,我有一个图像,我想用javascript打印它,但是JS中图像的id总是空的。请告诉我该怎么办 <div class="reportFieldsDiv" runat="server" clientidmode="Static"> <table style="width: 400px; margin: 10px 0px 0px 10px; border: 1px solid black">

在我的网页中,我有一个图像,我想用javascript打印它,但是JS中图像的id总是空的。请告诉我该怎么办

<div class="reportFieldsDiv" runat="server" clientidmode="Static">
                <table style="width: 400px; margin: 10px 0px 0px 10px; border: 1px solid black">
                    <tr>
                        <td>
                            <img src="~/Images/Layout/TestBadge.jpg" alt="ID Badge" id="mainImage"/>
                        </td>
                    </tr>
                </table>
                <div style="border: 0px solid black;padding:5px 0px 0px 280px">
                  <asp:Button ID="btnPrint" runat="server" Text="Print" Width="140px" />

                </div>
            </div>


    <script type="text/javascript">

        window.onload = function () {
            var image = document.getElementById('mainImage');
            printImage(image);
        };


        function printImage(image) {
            var printWindow = window.open('', 'Print Window', 'height=400,width=600');
            printWindow.document.write('<html><head><title>Print Window</title>');

            printWindow.document.write('<img id="mainImage" src=\'..\Images\Layout\TestBadge.jpg');
            printWindow.document.write(image.src);
            printWindow.document.write('\' /></body></html>');
            printWindow.document.close();
            printWindow.print();
        }


    </script>

window.onload=函数(){
var image=document.getElementById('mainImage');
打印图像(图像);
};
函数printImage(图像){
var printWindow=window.open(“”,'打印窗口','高度=400,宽度=600');
printWindow.document.write('Print Window');
printWindow.document.write(“”);
printWindow.document.close();
printWindow.print();
}

您没有将id添加到
打印窗口
文档,如下所示:

printWindow.document.write('<img id="mainImage" src="..\Images\Layout\TestBadge.jpg"');

printWindow.document.write('我相信如果您使用
runat=server
,ASP.NET Webforms将分配不同的ID。请使用浏览器的检查器检查图像的实际ID。这是一个快速修复方法,谢谢,但现在我想在弹出窗口中打开图像并触发打印事件,但由于某些原因,图像不会出现。您能帮助我吗?
RunaT=Server < /Calp>不适用于“经典ASP”,除了微软之外,它也不能工作。如果你不需要它,就不要这样做。.IMHO…由于某些原因,当弹出窗口出现时,我看不到图像。我如何修复它?我不知道为什么你的图像没有出现。我没有该图像,因此当我使用你的html时,它不会出现在我身上。我认为错误的一件事是你的引号在字符串中错误。我将编辑我的答案,使引号成为c纠正。