[英]The function doesn't work
I've got a problem with this code. 我有这个代码的问题。 When I wrote the first code everything is ok but after processing it doesn't work. 当我编写第一个代码时,一切正常,但经过处理后不起作用。 Why is it like that? 为什么会这样呢? I sent an event object to function as an argument so what's the problem? 我发送了一个事件对象作为参数,所以出了什么问题?
Initial code: 初始代码:
var isOkej = null;
function isNumber(someValue) {
return !isNaN(someValue);
}
window.onload = function () {
var wykonawca = document.getElementById("informations").artist;
var tytul = document.getElementById("informations").title;
var label = document.getElementById("informations").label;
var kindOftxt = document.getElementById("kindOftxt");
var action = function (e) {
//pokazuje unicode wpisanego znaku
var wpisanyZnak = e.which;
if (isNumber(this.value) || wpisanyZnak === 190) {
e.preventDefault();
if (this === wykonawca)
kindOftxt.innerHTML = "Podaj swój Alias";
else if (this === tytul)
kindOftxt.innerHTML = "Podaj tytuł utworu";
else
kindOftxt.innerHTML = "Gdzie utwór został wydany";
this.style.backgroundColor = "red";
isOkej = false;
} else {
this.style.backgroundColor = "green";
kindOftxt.innerHTML = "";
isOkej = true;
}
};
wykonawca.onkeyup = action;
tytul.onkeyup = action;
label.onkeyup = action;
}
Final code: 最终代码:
function isNumber(someValue) {
return !isNaN(someValue);
}
var isOkej = null;
function action (e, wykonawca, tytul,kindOftxt) {
//pokazuje unicode wpisanego znaku
var wpisanyZnak = e.which;
if (isNumber(this.value) || wpisanyZnak === 190) {
e.preventDefault();
if (this === wykonawca)
kindOftxt.innerHTML = "Podaj swój Alias";
else if (this === tytul)
kindOftxt.innerHTML = "Podaj tytuł utworu";
else
kindOftxt.innerHTML = "Gdzie utwór został wydany";
this.style.backgroundColor = "red";
isOkej = false;
} else {
this.style.backgroundColor = "green";
kindOftxt.innerHTML = "";
isOkej = true;
}
};
window.onload = function () {
var wykonawca = document.getElementById("informations").artist;
var tytul = document.getElementById("informations").title;
var label = document.getElementById("informations").label;
var kindOftxt = document.getElementById("kindOftxt");
wykonawca.onkeyup = function (e) {
action(e, wykonawca,tytul,kindOftxt);
};
tytul.onkeyup = function (e) {
action(e, wykonawca,tytul,kindOftxt);
};
label.onkeyup = function (e) {
action(e, wykonawca,tytul,kindOftxt);
}; }
I really don't know what can be the reason. 我真的不知道是什么原因。 What do you think can be a problem? 您认为可能有什么问题?
When you bind the events with the following code, the this
inside action
function is bound to the input element. 使用以下代码绑定事件时, this
内部action
函数将绑定到input元素。
wykonawca.onkeyup = action;
tytul.onkeyup = action;
label.onkeyup = action;
When you bind the events with the updated code 当您使用更新的代码绑定事件时
wykonawca.onkeyup = function (e) {
action(e, wykonawca,tytul,kindOftxt);
};
tytul.onkeyup = function (e) {
action(e, wykonawca,tytul,kindOftxt);
};
label.onkeyup = function (e) {
action(e, wykonawca,tytul,kindOftxt);
};
the this
inside the anonymous event handlers is bound to the input element, but in the action
function, this
would refer to the global object ie the window object. 匿名事件处理程序中的this
绑定到input元素,但是在action
函数中, this
将引用全局对象,即window对象。
You can pass the this
reference from your anonymous handlers to the action
function as an additional argument. 您可以this
引用从您的匿名处理程序作为附加参数传递给action
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.