admin管理员组文章数量:1290268
document.onkeydown = function(event) {
var tagName = event.target.tagName;
if (tagName != 'INPUT' && tagName != 'TEXTAREA' && !event.alt && event.control) {
if (event.ctrlKey && event.keyCode == 37) {
if (_this.currentPage > 1) {
window.location.href = _this.baseUrl.replace(/%page%/i, _this.currentPage + 1);
}
} else if (event.ctrlKey && event.keyCode == 39) {
if (_this.currentPage < _this.pagesTotal) {
window.location.href = _this.baseUrl.replace(/%page%/i, _this.currentPage - 1);
}
}
}
}
This gives me an error only in IE 8:
'target' is null or not an object
for that line var tagName = event.target.tagName;
How to fix that? Error happens when I press Ctrl or arrows button.
document.onkeydown = function(event) {
var tagName = event.target.tagName;
if (tagName != 'INPUT' && tagName != 'TEXTAREA' && !event.alt && event.control) {
if (event.ctrlKey && event.keyCode == 37) {
if (_this.currentPage > 1) {
window.location.href = _this.baseUrl.replace(/%page%/i, _this.currentPage + 1);
}
} else if (event.ctrlKey && event.keyCode == 39) {
if (_this.currentPage < _this.pagesTotal) {
window.location.href = _this.baseUrl.replace(/%page%/i, _this.currentPage - 1);
}
}
}
}
This gives me an error only in IE 8:
'target' is null or not an object
for that line var tagName = event.target.tagName;
How to fix that? Error happens when I press Ctrl or arrows button.
Share Improve this question edited Apr 25, 2022 at 7:37 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 12, 2012 at 2:03 user1815131user1815131 1012 silver badges7 bronze badges 4-
event.target
does not exist in IE8. Wele to IE's old event system. – Šime Vidas Commented Nov 12, 2012 at 2:06 - Please can you give an example how to fix that? – user1815131 Commented Nov 12, 2012 at 2:09
- Use a third-party event API what takes care of the cross-browser inpatibilities for you. That would be my advice. – Šime Vidas Commented Nov 12, 2012 at 2:11
- Yeah, but I am not good at all in Javascript. I need example what to change. – user1815131 Commented Nov 12, 2012 at 2:13
2 Answers
Reset to default 6IE does not pass in the event
object into the event handler. Instead, they use the global event
property of the window
object. So for IE, you'd use window.event
instead.
It is mon practice to test for the supplied argument first. You also have to take into account the fact the IE uses srcElement
instead of target
. To account for all that, use something similar to this:
document.onkeydown = function(event) {
event = event || window.event;
var tagName = (event.target || event.srcElement).tagName;
// Keep up the good work...
}
This should do the trick.
Do it like this:
event = event || window.event;
var tagName = (event.target || event.srcElement).tagName.toUpperCase();
本文标签: internet explorerJavascript IE error 39target39 is null or not an objectStack Overflow
版权声明:本文标题:internet explorer - Javascript IE error: 'target' is null or not an object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741495358a2381810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论