admin管理员组文章数量:1332345
I've been trying to make the game Battleship in JavaScript for a school assignment, and I'm stuck trying to create the opponent AI. I've created an event for when I click a cell in the grids:
function addListener(evt) {
evt.addEventListener('click', function(){
//bunch of code
});
}
Now I run this function addListener
every time I create a new cell in the grid in my nested for loop:
yourCell.setAttribute('id', evt + String(a) + String(b));
addListener(yourCell);
What I want now is for the opponent to run this click event when I've made my turn, so I wrote this just to test out the fireEvent
function:
function enemyTurn() {
document.getElementById('yourGrid00').fireEvent('onclick');
}
According to the second code example, I set the ID of the cell to 'yourGrid00'
, which I've confirmed by inspecting the html code after the JavaScript code has been run, and the function enemyTurn()
will never run before each cell has been created and assigned an ID.
What I do not understand is that I get the following error on the fireEvent
line:
Uncaught TypeError: undefined is not a function.
Does anyone know what I'm doing wrong?
I've been trying to make the game Battleship in JavaScript for a school assignment, and I'm stuck trying to create the opponent AI. I've created an event for when I click a cell in the grids:
function addListener(evt) {
evt.addEventListener('click', function(){
//bunch of code
});
}
Now I run this function addListener
every time I create a new cell in the grid in my nested for loop:
yourCell.setAttribute('id', evt + String(a) + String(b));
addListener(yourCell);
What I want now is for the opponent to run this click event when I've made my turn, so I wrote this just to test out the fireEvent
function:
function enemyTurn() {
document.getElementById('yourGrid00').fireEvent('onclick');
}
According to the second code example, I set the ID of the cell to 'yourGrid00'
, which I've confirmed by inspecting the html code after the JavaScript code has been run, and the function enemyTurn()
will never run before each cell has been created and assigned an ID.
What I do not understand is that I get the following error on the fireEvent
line:
Uncaught TypeError: undefined is not a function.
Does anyone know what I'm doing wrong?
Share Improve this question edited Oct 13, 2014 at 8:59 James Donnelly 129k35 gold badges214 silver badges223 bronze badges asked Oct 13, 2014 at 8:43 Torstein RøsokTorstein Røsok 931 gold badge1 silver badge6 bronze badges1 Answer
Reset to default 4fireEvent
is a really old method which is only supported on Internet Explorer versions 8 and lower. You should instead use dispatchEvent
if you want to handle up-to-date browsers.
Dispatches an
Event
at the specifiedEventTarget
, invoking the affectedEventListeners
in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) apply to events dispatched manually withdispatchEvent()
.
You have to create the Event
manually though - you can't just call dispatchEvent('onclick')
. I've pulled the first bit of the following code from this answer.
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 0, 0, 0, 0,
false, false, false, false, 0, null);
function enemyTurn() {
document.getElementById('yourGrid00').dispatchEvent(evt);
}
JSFiddle demo.
本文标签: htmlJavaScript fireEvent not a functionStack Overflow
版权声明:本文标题:html - JavaScript fireEvent not a function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742326982a2453922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论