我正在尝试使一个网站的用户脚本添加自定义表情。然而,我已经收到了很多错误。
下面是函数:
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
调用此函数:
function appendEmote(em) {
shoutdata.value += em;
}
每次单击具有onclick
属性的按钮时,我都会收到以下错误:
未捕获ReferenceError:未定义函数。
更新
我尝试使用:
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。
我试着这样做是为了测试监听器是否对我有效:
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&popup=true&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);
发布于 2020-11-23 21:31:28
确定你是否在使用Javascript模块?!如果使用js6模块,您的html事件属性将不起作用。在这种情况下,您必须将您的函数从全局作用域引入模块作用域。只需将以下代码添加到您的javascript文件:window.functionName= functionName;
示例:
<h1 onClick="functionName">some thing</h1>
发布于 2020-12-05 11:30:15
我认为您将函数放在$(.ready)文档中......函数总是在$(.ready)文档中提供.......
发布于 2020-12-15 14:45:54
我在特定组件的.html文件中用(click) = "someFuncionName()"
在angular中解决了这个问题。
https://stackoverflow.com/questions/17378199
复制相似问题