簡體   English   中英

如何獲取動態表格ID值並將其傳遞給javascript函數

[英]How to get the dynamic table id value and to pass it to javascript function

我需要您的幫助來獲取表的ID值並將該值傳遞給javascript函數。 我在表格中有一個圖片,我想點擊它,要捕獲的表格ID並將其傳遞給javascript函數。 這是表代碼:

<table class="tablesorter" id="detailsTable<%=tableCounter%>" width="80%">
<tr>
<td>
<a href="javascript:fnExcelReport();">
<img border="0" src="../Images/excel_icon.png" alt="Export to Excel"></a>
</td>
</tr>
</table>

還有需要將ID傳遞給它的javascript函數(第7行):

<script type="text/javascript">
function fnExcelReport()
{

    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j=0;
    tab = document.getElementById('detailsTable1'); // id of table

    for(j = 0 ; j < tab.rows.length ; j++) 
    {     
        tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
        //tab_text=tab_text+"</tr>";
    }

    tab_text=tab_text+"</table>";
    tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
    tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
    tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE "); 

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
    {
        txtArea1.document.open("txt/html","replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus(); 
        sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
    }  
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}

</script>

這樣就解決了問題:

  <table class="tablesorter" id="detailsTable<%=tableCounter%>" width="80%">
            <tr>
                <td>
                    <img onclick="fnExcelReport(this);" border="0" src="../Images/excel_icon.png" alt="Export to Excel">
                </td>
            </tr>
        </table>

和javascript代碼:

 function fnExcelReport(ele) {
                var tableid = ele.parentNode.parentNode.parentNode.parentNode.id;
                // etc.
            }

您必須獲取圖像(觸發)的parentElement.id

您可以向圖像添加一個class ,以確保這是所需的圖像。

快速而骯臟的代碼段:

<img class="trigger" border="0" src="../Images/excel_icon.png" alt="Export to Excel"></a>

window.addEventListener("click", function(e){ //instead of window you can also add your element
    if (e.target.className == "trigger"){
        var table = e.target.parentElement.parentElement.parentElement.parentElement;
        fnExcelReport(table.id;);

        }
    })

然后:

function fnExcelReport(id)
{

    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j=0;
    tab = document.getElementById(id); // id of table

...

暫無
暫無

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

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