won*_*ng2 5 javascript javascript-events
我想知道输入元素是否已经改变,我知道我可以onpropertychange在IE和oninput其他浏览器中收听.
这是我的代码:
var _addChangedProperty = function(input){
input.changed = false;
var oninput = function(){
this.changed = !!this.value;
if(this.changed){
this.style.color = "black";
}
};
input.onpropertychange = input.oninput = oninput;
};
Run Code Online (Sandbox Code Playgroud)
现在我想改input.onpropertychange = input.oninput = oninput;到addEventListerner和attachEvent,我需要检查,如果onpropertychange是支持的事件,我怎么能做到这一点(没有浏览器检测)?
您可以使用in运算符进行检查:
"onpropertychange" in input
Run Code Online (Sandbox Code Playgroud)
这种功能测试在旧版本的 Firefox 中不起作用,旧版本的 Firefox 会报告与false确实存在的事件相对应的事件处理程序属性的事件,但这在这里不是问题,因为 Firefox 目前不支持该propertychange事件,并且不太可能将来。
这是一些背景:http ://perfectionkills.com/detecting-event-support-without-browser-sniffing/
另一点:您需要单独的函数来处理propertychange和input事件,因为在propertychange处理程序中您需要检查属性是否value已更改。否则,您最终将处理对输入的任何属性的更改。
input.onpropertychange = function(evt) {
evt = evt || window.event;
if (evt.propertyName == "value") {
// Do stuff here
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2018 次 |
| 最近记录: |