簡體   English   中英

在onclick上運行此參數

[英]Function this parameter on onclick

我有一些像這樣在javascript中生成的html代碼

cell.innerHTML = '<a href="#" class="sortheader" id="sortheader_'+i+'" '+ 
        'onclick="ts_resortTable(this, '+i+');return false;">' + 
        txt+'<span class="sortarrow"></span></a>';

我想調用函數ts_resortTable(),但獨立於onclick事件如何生成函數的“ this”參數?

我嘗試這樣做

ts_resortTable.call(document.getElementById('sortheader_'+i), i);

和這個

ts_resortTable(document.getElementById('sortheader_'+i), i);

但這沒用

我想調用的功能是這樣的:

function ts_resortTable(lnk,clid) {

    //save clid in cookies
    $.cookie("clid", clid);

    //fade to table
    $('#table8k6k_progress').before('<div id="loading" style="background: #fff; position: absolute; margin-top: -25px; padding: 3px 5px; font-weight: bold;">Loading...</div>');
    $('#table8k6k_progress').fadeTo('fast',0.5, function() {
        TIMERSTART = (new Date()).getTime();
        // get the span
        var span;
        for (var ci=0;ci<lnk.childNodes.length;ci++) {
            if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
        }
        var spantext = ts_getInnerText(span);
        var td = lnk.parentNode;
        var column = clid || td.cellIndex;
        var table = getParent(td,'TABLE');

        // Work out a type for the column
        if (table.rows.length <= 1) return;
        var itm = ts_getInnerText(table.rows[1].cells[column]);
        //itm = itm.substr(0,7);
        sortfn = ts_sort_caseinsensitive;
        if (itm.match(/^\d{1,2}[\/-]\d{1,2}[\/-]\d{2,4}\s\d\d\W\d\d$/)) sortfn = ts_sort_date;
        if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
        if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
        if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
        if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;
        SORT_COLUMN_INDEX = column;
        var firstRow = new Array();
        var newRows = new Array();
        for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
        for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }

        newRows.sort(sortfn);

        if (span.getAttribute("sortdir") == 'down') {
            ARROW = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
            newRows.reverse();
            span.setAttribute('sortdir','up');
        } else {
            ARROW = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
            span.setAttribute('sortdir','down');
        }

        // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
        // don't do sortbottom rows
        for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
        // do sortbottom rows only
        for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}

        // Delete any other arrows there may be showing
        var allspans = document.getElementsByTagName("span");
        for (var ci=0;ci<allspans.length;ci++) {
            if (allspans[ci].className == 'sortarrow') {
                if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
                    allspans[ci].innerHTML = '';
                }
            }
        }

        span.innerHTML = ARROW;

        //fadeTo
        $('#table8k6k_progress').fadeTo('fast', 1);

        //remove div loading
        $('#loading').remove();
    });


    //alert("Time taken: " + ( (new Date()).getTime() - TIMERSTART ) + "ms");

}

要調用函數,並明確控制哪些this將是里面的功能,使用該功能的調用方法

ts_resortTable.call(document.getElementById('sortheader_'+i), i)
  • 上述將設置this到的document.getElementById函數內(+ I“_ sortheader”)。
  • 該函數還將接收一個參數, i

根據您輸入的內容

$('sortheader_' + i)

由於不是MooTools,因此需要為選擇器執行此操作。

$('#sortheader_' + i)

您需要在jQuery中使用CSS選擇器。

通過致電

onclick="ts_resortTable(this, '+i+');

ts_resortTable函數的第一個參數將是<a>標記itelf

document.getElementById('sortheader _'+ i)

當我在文檔就緒事件上啟動功能時,尚未生成具有ID的鏈接。 因此解決方案是延遲功能的啟動;)

暫無
暫無

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

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