首页 > 解决方案 > selObj.fireEvent 不是函数

问题描述

当用户单击父项时,我们在 js 文件中使用此功能来拉菜单树:

function menusel(unid) 
{   
    //if its in the array, deactivate it and take it out
    var index = inarray(unid,selectedarray);

    //first arrange the array
    if(index)
    {
        selectedarray[index] = 0;
    }
    else
    {
        //we have restrictions
        if(treeRestrictMode==1)
        {
            //now check its not in the array of items not allowed to b picked
            if(inarray(unid,nonSelArr))
            {
                alert('This Item is Unselectable'); 
                return;
            }       
        }
        
        //if we are in unique tree mode, can only select one
        if(treeSingleMode==1)
        {       
            //check for a non zero value, and deselect it [recursively]#
            for(var x=0;x<selectedarray.length;x++)
            {
                if(selectedarray[x]!=0)
                {   
                    //alert(unid+' '+selectedarray[x]);
                    menusel(selectedarray[x]);
                }
            }
        }
        
        selectedarray.push(unid);
    }
    
    sel_unselect(unid,index);

    //if we have an override function, it means we will assume that the parent page
    //as an input type function called treeSelFunc which takes the id and takes care of the rest
    if(overrideFunction!='')
    {
        parent.parent.treeSelFunc(unid,selName);
        return;
    }

    if(treeSingleMode!=1)
    {
        var selObj = emptySel(0);//set txt will fill it for us
    }
    else
    {
        var selObj = parent.parent.MM_findObj(selName);
        
        //see if we have options..will need to empty them
        if(selObj.size>1)
        {
            emptySel(1);
        }
        else
        {
            selObj.value=0;
        }
        
    }
    setTxt(selObj);
    selObj.fireEvent('onchange');   
}

我们在页面上这样称呼它:

<a id="troot[VMENU]_040" onclick="menusel(40);" class="sel">All</a>

我们得到了这个: Uncaught TypeError: selObj.fireEvent is not a function

它曾经可以在较旧的浏览器版本上运行(我认为是 explorer 8),但现在不再使用 Chrome。任何想法为什么?

标签: javascripteventsruntime-error

解决方案


fireEvent 方法是一种专有的 Microsoft Internet Explorer 替代标准 EventTarget.dispatchEvent() 方法。

请改用 dispatchEvent 方法。

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent


推荐阅读