需要将参数和事件信息传递给javascript函数

时间:2012-05-01 15:43:44

标签: javascript javascript-events

下面列出的是我的javascript函数。该功能在IE上运行正常但由于包含window.event部分而无法在Firefox上运行。这样做的方法是在IE以外的其他浏览器中工作,就是在函数头的参数列表中使用参数e(或其他未定义的东西)。如果已有其他参数,如何将参数添加到参数?下面列出的是从应用程序到函数的示例调用。再次感谢。

function entityclick(where) {
    try {
        var tempstring = new String(window.event.srcElement.id)
    }
    catch (oExp) {
        var tempstring = new String("noclickyclicky")
    }
    tempstring = tempstring.substr(0, 3)
    if (tempstring != "img") {
        if (selectedentity != "") {
            try {
                document.getElementById('tbl' + selectedentity).style.backgroundColor = ""
            }
            catch (oExc) {
            }
        }
        selectedentity = where.EntityID + where.EntityCat;
        var EntityID = where.EntityID
        var EntityCat = where.EntityCat
        var ParentEntityID = where.ParentEntityID
        var ParentEntityCat = where.ParentEntityCat
        var projtype = 0
        document.getElementById('tbl' + selectedentity).style.backgroundColor = "lightsteelblue"
        //document.all('tbl'+selectedentity).className = "HighlightMe"
        ifram = document.getElementById('detailframe')
        if (0 == 0) {
            if (EntityCat == 35)
                ifram.src = "../teams/Viewteam.aspx?taskgrpid=" + EntityID + "&taskgrpcategory=" + EntityCat + "&ptaskgrpid=" + ParentEntityID + "&ptaskgrpcategory=" + ParentEntityCat + "&taskgrptype=" + GLOBALTASKGRPTYPE;
            else if (EntityCat == 26)
                ifram.src = "../teams/Viewteam.aspx?taskgrpid=" + EntityID + "&taskgrpcategory=" + EntityCat + "&ptaskgrpid=" + ParentEntityID + "&ptaskgrpcategory=" + ParentEntityCat + "&taskgrptype=" + GLOBALTASKGRPTYPE;
            else if (EntityCat == 34)
                ifram.src = "../teams/Viewteam.aspx?taskgrpid=" + EntityID + "&taskgrpcategory=" + EntityCat + "&ptaskgrpid=" + ParentEntityID + "&ptaskgrpcategory=" + ParentEntityCat + "&taskgrptype=" + GLOBALTASKGRPTYPE;
            else if (EntityCat == 10)
                ifram.src = "../solutions/ViewSolution.aspx?taskgrpid=" + EntityID + "&taskgrpcategory=" + EntityCat + "&ptaskgrpid=" + ParentEntityID + "&ptaskgrpcategory=" + ParentEntityCat + "&taskgrptype=" + GLOBALTASKGRPTYPE;
            else if (EntityCat == 11)
                ifram.src = "../observations/ViewObservation.aspx?taskgrpid=" + EntityID + "&taskgrpcategory=" + EntityCat + "&ptaskgrpid=" + ParentEntityID + "&ptaskgrpcategory=" + ParentEntityCat;
            else if (EntityCat == 12)
                ifram.src = "../actions/ViewAction.aspx?taskgrpid=" + EntityID + "&taskgrpcategory=" + EntityCat + "&ptaskgrpid=" + ParentEntityID + "&ptaskgrpcategory=" + ParentEntityCat;
            else if (EntityCat == 13)
                ifram.src = "../tasks/ViewTask.aspx?taskgrpid=" + EntityID + "&taskgrpcategory=" + EntityCat + "&ptaskgrpid=" + ParentEntityID + "&ptaskgrpcategory=" + ParentEntityCat;
            ifram.style.left = '27%'
            ifram.style.display = 'block'
            //detail.style.display = 'none'
            ifram.style.zIndex = 5
        }
    }
}

这是对函数的调用

entityclick(document.getElementById('tbl' + glbRefreshID + glbRefreshCAT))

编辑:在某些地方也会以其他方式调用entityclick,例如

entityclick(this)

entityclick(document.getElementById('tbl' + GlobalExplodeID + GlobalExplodeCat))

2 个答案:

答案 0 :(得分:0)

使用额外函数作为事件处理程序。例如

function handler(e) {
    if (!e)
        e = window.event;
    entityclick(e.target, e); // have more than one parameter in your fn
}
var elem = document.getElementById("xyz");
if (elem.addEventHandler)
    elem.addEventHanlder("click", handler, false);
else
    elem.attachEvent("onclick", handler);

function entityclick(elem, e) {
/* get: a DOM node with special properties[, an Event object] */
     if (e)
          // we handle something
     else
          // we have to use defaults
}

答案 1 :(得分:0)

能够使用此帖子的提示进行解决

http://www.sitepoint.com/forums/showthread.php?330837-window-event-is-not-working-in-Firefox

可以将事件传递给同一个函数,然后让我仍然可以获得我需要的所有项目。感谢所有观看/帮助的人。