1
我正在编写一个UserScript,一切正常,直到现在,当我点击一个按钮时,我应用了一个onClick属性,控制台一直告诉我Uncaught ReferenceError: SB_Change is not defined
和功能SB_Change(what)
将不会执行。Uncaught ReferenceError:<我的用户脚本定义的函数>未定义
代码:
window.addEventListener('load', function() {
var but1 = document.createElement("BUTTON");
var t1=document.createTextNode(" + ");
but1.style.width = "16px";
but1.style.height = "16px";
but1.appendChild(t1);
but1.setAttribute('onclick','SB_Change("+")');
// -
var but2 = document.createElement("BUTTON");
var t2=document.createTextNode(" - ");
but2.appendChild(t2);
but2.style.width = "16px";
but2.style.height = "16px";
but2.setAttribute('onclick','SB_Change("-")');
// -
var SB_text = document.getElementById("shoutbox").getElementsByTagName("h3")[0];
SB_text.innerHTML = "Shoutbox ";
SB_text.appendChild(but1);
SB_text.appendChild(but2);
function SB_Change(type){
var H = document.getElementById("sbPosts").maxHeight;
switch(type){
case "+":
H = "800px";
break;
case "-":
H = "400px";
break;
}
}
}, false);
谢谢你,我喜欢你说的,但它不工作。也许是因为我的功能有争议?反正继承我新的代码:http://pastebin.com/LQmn1NEr它不告诉我“SB_Change没有定义..:”当我点击一个按钮时,它什么也不做,当我输入SB_Change(“+ “)进入控制台,比它声明SB_Change未定义 – PoTTii 2014-09-05 18:15:06
新代码不正确地使用'addEventListener'。另外,一般来说,您不能在控制台中使用userscript函数。但是,在这种情况下,您可以通过将其定义为:'window.SB_Change = function(type){'。无论如何,你需要为这些新问题开一个新的问题。 – 2014-09-05 23:43:06