onfocus&onblur无法在IE中工作
问题描述:
我写过一些代码,可以让我在输入表单上显示一些灰色的文本,当用户关注它时,文本会消失并变得没有任何意义,如果有道理...onfocus&onblur无法在IE中工作
function editableAlt (elem, colour1, colour2) {
var elem = document.getElementById(elem);
elem.onfocus = function() {
if(this.value == this.defaultValue) {
this.value = ""; // remove the default text, so the user can enter their own
this.style.color = "#" + colour1; // change the text colour
}
};
elem.onblur = function() {
if(this.value == '') {
this.value = this.defaultValue; // user left it blank? revert back to the default text
this.style.color = "#" + colour2; // and change the colour back too
}
}
}
这是工作在大多数页面上,但在一个由于某种原因onfocus和的onblur是不工作 - 如果我改变它例如以它的onclick触发没有问题。
还有什么我可以尝试吗? :(我使用JQuery,但除去这似乎并没有在所有实现它。
干杯
答
我发现IE浏览器是非常无情的,当涉及到JavaScript错误。我跑你的代码通过JSLint的,并得到了如下错误:
错误:在第2行的字符14 问题: 'ELEM' 已定义
var elem = document.getElementById(elem);
PROBL em在第10行字符23:使用'==='与''比较。
if(this.value == '') {
问题在第14行字符6:缺少分号。
}
隐含全球:文件2
不是要吝啬帮助您的或任何东西,但我猜,如果你修复这些错误,你会看到更理想的结果。祝你好运!
答
真是个白痴,对不起家伙!我试图删除所有其他的Javascript文件,但错过了最重要的一个,它被封装在PHP和IE中,如果声明,德哦!
口鱼sfFocus正在施加导致它打破:http://htmldog.com/articles/suckerfish/shoal/
对不起:(&您的建议
作品在我的IE6千恩万谢:http://jsbin.com/aduni你做。东西在你的代码的其他错误别处 – 2009-09-25 15:12:08
谢谢,我甚至试过这种使用jQuery无济于事: \t \t如果($( “#Absence_Hours”)长度> 0){ \t \t \t $此= $(本); //缓存变量 \t \t \t $ this.focus(函数(){ \t \t \t \t警报( '试验'); \t \t \t}); \t \t} 改变焦点()点击()工作正常,但... – Nick 2009-09-28 09:42:09