首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >未捕获ReferenceError:函数未使用onclick定义

未捕获ReferenceError:函数未使用onclick定义
EN

Stack Overflow用户
提问于 2013-06-29 16:18:56
回答 6查看 467.6K关注 0票数 102

我正在尝试使一个网站的用户脚本添加自定义表情。然而,我已经收到了很多错误。

下面是函数:

代码语言:javascript
复制
function saveEmotes() {
    removeLineBreaks();
    EmoteNameLines = EmoteName.value.split("\n");
    EmoteURLLines = EmoteURL.value.split("\n");
    EmoteUsageLines = EmoteUsage.value.split("\n");

    if (EmoteNameLines.length == EmoteURLLines.length && EmoteURLLines.length == EmoteUsageLines.length) {
        for (i = 0; i < EmoteURLLines.length; i++) {
            if (checkIMG(EmoteURLLines[i])) {
                localStorage.setItem("nameEmotes", JSON.stringify(EmoteNameLines));
                localStorage.setItem("urlEmotes", JSON.stringify(EmoteURLLines));
                localStorage.setItem("usageEmotes", JSON.stringify(EmoteUsageLines));
                if (i == 0) {
                    console.log(resetSlot());
                }
                emoteTab[2].innerHTML += '<span style="cursor:pointer;" onclick="appendEmote(\'' + EmoteUsageLines[i] + '\')"><img src="' + EmoteURLLines[i] + '" /></span>';
            } else {
                alert("The maximum emote(" + EmoteNameLines[i] + ") size is (36x36)");
            }
        }
    } else {
        alert("You have an unbalanced amount of emote parameters.");
    }
}

span标记的onclick调用此函数:

代码语言:javascript
复制
function appendEmote(em) {
    shoutdata.value += em;
}

每次单击具有onclick属性的按钮时,我都会收到以下错误:

未捕获ReferenceError:未定义函数。

更新

我尝试使用:

代码语言:javascript
复制
emoteTab[2].innerHTML += '<span style="cursor:pointer;" id="'+ EmoteNameLines[i] +'"><img src="' + EmoteURLLines[i] + '" /></span>';
document.getElementById(EmoteNameLines[i]).addEventListener("click", appendEmote(EmoteUsageLines[i]), false);

但是我得到了一个undefined错误。

这是script

我试着这样做是为了测试监听器是否对我有效:

代码语言:javascript
复制
emoteTab[2].innerHTML = '<td class="trow1" width="12%" align="center"><a id="togglemenu" style="cursor: pointer;">Custom Icons</a></br><a style="cursor: pointer;" id="smilies" onclick=\'window.open("misc.php?action=smilies&amp;popup=true&amp;editor=clickableEditor","Smilies","scrollbars=yes, menubar=no,width=460,height=360,toolbar=no");\' original-title="">Smilies</a><br><a style="cursor: pointer;" onclick=\'window.open("shoutbox.php","Shoutbox","scrollbars=yes, menubar=no,width=825,height=449,toolbar=no");\' original-title="">Popup</a></td></br>';
document.getElementById("togglemenu").addEventListener("click", changedisplay,false);
EN

回答 6

Stack Overflow用户

发布于 2020-11-23 21:31:28

确定你是否在使用Javascript模块?!如果使用js6模块,您的html事件属性将不起作用。在这种情况下,您必须将您的函数从全局作用域引入模块作用域。只需将以下代码添加到您的javascript文件:window.functionName= functionName;

示例:

代码语言:javascript
复制
<h1 onClick="functionName">some thing</h1>
票数 8
EN

Stack Overflow用户

发布于 2020-12-05 11:30:15

我认为您将函数放在$(.ready)文档中......函数总是在$(.ready)文档中提供.......

票数 4
EN

Stack Overflow用户

发布于 2020-12-15 14:45:54

我在特定组件的.html文件中用(click) = "someFuncionName()"在angular中解决了这个问题。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17378199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档