问题描述
我使用下面的脚本来替换从传统标准形式的一些变音符号。在Firefox以外的所有浏览器中都能正常工作。它根本不工作。我认为这个问题来自event.target,但我刚刚开始使用JavaScript,并缺乏知道如何解决这个问题。发现这个(posible?问题,但不知道如何适应我的脚本:
任何帮助将不胜感激!
function inlocuire_diacritice(form){
var form = event.target;
var i,l;
for(i = 0,l = form.elements.length; i< l; i + = 1){
if(form.elements [i] .type ==='text') {
form.elements [i] .value = form.elements [i] .value.replace(/Ş/ g,'Ş');
form.elements [i] .value = form.elements [i] .value.replace(/ş/ g,'ş');
form.elements [i] .value = form.elements [i] .value.replace(/Ţ/ g,'Ţ');
form.elements [i] .value = form.elements [i] .value.replace(/ţ/ g,'ţ');
}
}
返回true;
$ b 编辑:我从另一个函数内部调用函数:$ / b
函数validate(form){
//表单验证脚本
inlocuire_diacritice(form);
$ b $ p 这个表单在表单上调用:
< form action =whatevername =whatevermethod =postonsubmit =return validate(this)>
解决方案试试这个:
< form action =whatevername =whatevermethod =postonsubmit =return validate(event,this)> ... < /形式>
和函数:
pre > function inlocuire_diacritice(ev,form){
// form是对表单元素的引用
//如果您需要event.target,请使用ev.target
//这个引用了这个事件的元素
$ c
$ b实际上,在FF(或任何现代浏览器)中,调用函数时不需要传递参数,函数声明中需要包含一个参数,它代表 event
。 这个eventhandler中的
是指事件附加到的元素。
请阅读看,听众应该如何真正地附加到元素。
I use the script below to replace some diacritical marks from legacy to standard form. Works fine in all browsers, except Firefox; where it does not work at all. I assume the problem is from event.target, but I am just beginning with javascript and lack the know how for fixing this.
Found this (posible?) solution to my problem, but don't know how to adapt it for my script: event.target not working on Firefox
Any help would be greatly appreciated!
function inlocuire_diacritice (form) {
var form = event.target;
var i, l;
for (i = 0, l = form.elements.length; i < l; i += 1) {
if (form.elements[i].type === 'text') {
form.elements[i].value = form.elements[i].value.replace(/Ş/g, 'Ș');
form.elements[i].value = form.elements[i].value.replace(/ş/g, 'ș');
form.elements[i].value = form.elements[i].value.replace(/Ţ/g, 'Ț');
form.elements[i].value = form.elements[i].value.replace(/ţ/g, 'ț');
}
}
return true;
}
EDIT: I call the function from inside another function:
function validate(form) {
//form validation script
inlocuire_diacritice (form);
}
And this is called on form submit:
<form action="whatever" name="whatever" method="post" onsubmit="return validate(this)">
解决方案 Try this:
<form action="whatever" name="whatever" method="post" onsubmit="return validate(event, this)">...</form>
And the function :
function inlocuire_diacritice (ev, form) {
// form is a reference to the form element already
// if you need event.target, use ev.target
// this refers to the element to which this event is attached
:
}
Actually in FF (or in any modern browser) you don't need to pass arguments when invoking the function, in the function declaration you need to include one argument, which represents event
. this
in eventhandler refers to the element, to which the event is attached.
Please read about addEventListener
to see, how the listeners should actually be attached to elements.
这篇关于问题与Firefox:event.target的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!