簡體   English   中英

通過ID訪問DOM元素

[英]Accessing DOM element by Id

我有一個頁面,如果您單擊INV001鏈接,它將打開一個彈出窗口。 我正在嘗試使用document.getElementById('foo')訪問popUp窗口中的foo對象,但獲取了null

以下是網頁代碼

<tbody class="">
    <tr class="tipAdded">
        <td class="first">
            <div style="display:none" class="renderedValue">
              INV001
            </div><input value="2" name="instanceId0.id" type="hidden" class="check">
        </td>
        <td class="typeString  ">
            <div class="data typeString">
                <a tooltip="" class="linkedOverlay " title="" href="/CP/user/show.do?screenId=273&amp;mode=show&amp;referringAttributeId=1329&amp;key=&amp;entityInstanceId=2&amp;parentEntityId=&amp;parentInstanceId=&amp;parentInstanceKey=&amp;parentFieldName=">
                    INV001        
              </a>
            </div>
        </td>
    </tr>
</tbody>


<div class="ui">
    <div class="literal">
        <div id="one">
<object id="foo" name="foo" type="text/html" data="../../QuickInvoice/printInvoiceSummary.do?InvoiceId=2" style="height:700px;width:750px"></object>
</div>
    </div>
</div>

彈出窗口的代碼

<head>
        <title> Invoice Summary </title>

                <script type="text/javascript">
                 function getPrintData()
                {
                    var invoice = document.getElementByID('foo');
                    printHTML(invoice.data);

                }

function printHTML(htmlString) {
    var newIframe = document.createElement('iframe');
    newIframe.width = '1px';
    newIframe.height = '1px';
    newIframe.src = 'about:blank';

    // for IE wait for the IFrame to load so we can access contentWindow.document.body
    newIframe.onload = function() {
        var script_tag = newIframe.contentWindow.document.createElement("script");
        script_tag.type = "text/javascript";
        var script = newIframe.contentWindow.document.createTextNode('function Print(){ window.focus(); window.print(); }');
        script_tag.appendChild(script);

        newIframe.contentWindow.document.body.innerHTML = htmlString;
        newIframe.contentWindow.document.body.appendChild(script_tag);

        // for chrome, a timeout for loading large amounts of content
        setTimeout(function() {
            newIframe.contentWindow.Print();
            newIframe.contentWindow.document.body.removeChild(script_tag);
            newIframe.parentElement.removeChild(newIframe);
        }, 200);
    };
    document.body.appendChild(newIframe);
}
                </script>
    </head>
    <body>
        <caption align="left"><h2>Invoice Summary</h2></caption>
         <div style="position:absolute;top:0;right:0">
            <tbody>
                    <tr>
                        <th>
                            <form>
                                <input id='printWindow' type='button' onClick="getPrintData()" value='Print' class='NonPrintable' />
                            </form>
                        </th>
                    </tr>
            </tbody>        
        </div>  
        <table cellspacing='0' id='header'>     
            <tr>
              <th>Tracking Id</th>
              <td><%=invoice.get("Reference ID").toString()%></td>            
            </tr>
            <tr>
              <th>Upload Date</th>
              <td><%=invoice.get("Upload Date").toString()%></td>             
            </tr>
            <tr>
              <th>Status</th>
              <td><%=invoice.get("Status").toString()%></td>              
            </tr>           
        </table>                

    </body>
</html>

foo是彈出窗口的ID,在getPrintData中invoice值為null,有人可以幫助我在彈出窗口中訪問此foo嗎?

由於當前已實現,所以document.getElementByID('foo')試圖在彈出文檔(printInvoiceSummary.do)的上下文中查找元素“ foo”,該彈出文檔當前沒有id為“ foo”的元素。 如果我理解正確,則只需要訪問父文檔上ID為“ foo”的對象:

var invoice = top.document.getElementById('foo');

或者,您也可以使用以下命令訪問同一對象:

var invoice = window.frameElement;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM