java script初心者です。
google chrome のdocument.getElementByIdを取得したいのですが、
ご教授願えないでしょうか?
全コードを書きます。
function DomToolkit() { if(document.all) { // IE Layer.prototype.setVisibility = function(f) { this.div.style.visibility = (f ? "visible" : "hidden"); }; Layer.prototype.getX = function() { return this.div.style.pixelLeft; }; Layer.prototype.getY = function() { return this.div.style.pixelTop; }; Layer.prototype.moveTo = function(x, y) { this.div.style.pixelLeft = x; this.div.style.pixelTop = y; }; Layer.prototype.moveBy = function(dx, dy) { this.div.style.pixelLeft += dx; this.div.style.pixelTop += dy; }; this.getLayer = function(id) { return new Layer(document.all[id]); }; } else if(document.getElementById) { // Mozilla chrome Layer.prototype.setVisibility = function(f) { this.div.style.visibility = (f ? "visible" : "hidden"); }; Layer.prototype.getX = function() { return parseInt(this.div.style.left); }; Layer.prototype.getY = function() { return parseInt(this.div.style.top); }; Layer.prototype.moveTo = function(x, y) { this.div.style.left = x; this.div.style.top = y; }; Layer.prototype.moveBy = function(dx, dy) { this.div.style.left = parseInt(this.div.style.left) + dx; this.div.style.top = parseInt(this.div.style.top) + dy; }; this.getLayer = function(id) { return new Layer(document.getElementById(id)); }; } else { // NN4 Layer.prototype.setVisibility = function(f) { this.div.visibility = (f ? "show" : "hide"); }; Layer.prototype.getX = function() { return this.div.left; }; Layer.prototype.getY = function() { return this.div.top; }; Layer.prototype.moveTo = function(x, y) { this.div.moveTo(x, y); }; Layer.prototype.moveBy = function(dx, dy) { this.div.moveBy(dx, dy); }; this.getLayer = function(id) { var i = arguments.length - 1; var layer = document.layers[arguments[i--]]; while(i >= 0 && layer) { layer = layer.layers[arguments[i--]]; } return new Layer(layer); }; } } function Layer(div) { this.div = div; } var domtool; // DOM依存のツール var left, top, width, height, cell; var mv = 100; var pathinfo; var reloading = false; var ctrlbreak = false; var movingID = null; var CilckFlag1=0; function init(x, y, w, h, c, t) { domtool = new DomToolkit(); left = x; top = 0; width = w; height = 288; cell = c; pathinfo = location.search.replace(/&px=\d*|&py=\d*|&ctrl=\d*|&?msg=[^&]*/ig, ""); setTimeout("reloadFrame()", t * 1000); setInterval("breakControl()", 60); setTimeout("initEventHandler()", 100); } function initEventHandler() { // 入力フレームの生成待ち if(!parent.input || !parent.input.document || !parent.input.document.cursor || !parent.input.document.cursor.down) { setTimeout("initEventHandler()", 100); return; } if(document.all) { // IE document.onkeypress = handleKeypress; with(parent.input) { document.cursor.left.onmousedown = handleMouseDown; document.cursor.right.onmousedown = handleMouseDown; document.cursor.up.onmousedown = handleMouseDown; document.cursor.down.onmousedown = handleMouseDown; document.onmouseup = handleMouseUp; } } else if(document.getElementById) { // Mozilla document.addEventListener("keypress", handleKeypress, true); with(parent.input) { document.cursor.left.addEventListener("mousedown", handleMouseDown, true); document.cursor.right.addEventListener("mousedown", handleMouseDown, true); document.cursor.up.addEventListener("mousedown", handleMouseDown, true); document.cursor.down.addEventListener("mousedown", handleMouseDown, true); document.addEventListener("mouseup", handleMouseUp, false); } } else { // NN4 document.onkeypress = handleKeypress; document.captureEvents(Event.KEYPRESS); with(parent.input) { document.cursor.left.onmousedown = handleMouseDown; document.cursor.right.onmousedown = handleMouseDown; document.cursor.up.onmousedown = handleMouseDown; document.cursor.down.onmousedown = handleMouseDown; document.onmouseup = handleMouseUp; document.captureEvents(Event.MOUSEUP); } } } function handleKeypress(event) { if(ctrlbreak) { return false; } ctrlbreak = true; if(document.all) { key = window.event.keyCode; } else { key = event.which; } moveChar(String.fromCharCode(key)); } function breakControl() { ctrlbreak = false; } function handleMouseDown(event) { if(movingID != null) { clearInterval(movingID); movingID = null; } if(document.all) { button = parent.input.event.srcElement.name; } else { button = event.target.name; } switch(button) { case 'left': keych = '4'; break; case 'right': keych = '6'; break; case 'up': keych = '8'; break; case 'down': keych = '2'; break; default: keych = '5'; } movingID = setInterval("moveChar('" + keych + "')", 60); } function handleMouseUp(event) { if(movingID != null) { clearInterval(movingID); movingID = null; } } function reloadFrame() { if(reloading) { return; } reloading = true; layer = domtool.getLayer("char0"); x = layer.getX(); y = layer.getY(); location.search = pathinfo + "&px=" + x + "&py=" + y; } function moveChar(key) { var key2 = key; if(reloading) { return; } layer = domtool.getLayer("char0"); x = layer.getX(); y = layer.getY(); dx = 0; dy = 0; switch(key2) { case '4': // left dx = -mv; if(x + dx < left) { location.search = pathinfo + "&px=" + x + "&py=" + y + "&ctrl=4"; reloading = true; } break; case '6': // right dx = mv; if(x + dx + cell > left + width) { location.search = pathinfo + "&px=" + x + "&py=" + y + "&ctrl=6"; reloading = true; } break; case '8': // up dy = -mv; // if(y + dy < top) { if(y + dy < 0) { location.search = pathinfo + "&px=" + x + "&py=" + y + "&ctrl=8"; reloading = true; } break; case '2': // down dy = mv; // if(y + dy + cell > top + height) { if(y + dy + cell > 288 ) { location.search = pathinfo + "&px=" + x + "&py=" + y + "&ctrl=2"; reloading = true; } break; } if(!reloading) { layer.moveBy(dx, dy); layer2 = domtool.getLayer("balloon0"); layer3 = domtool.getLayer("text0"); if(layer2.div && layer3.div) { layer2.moveBy(dx, dy); layer3.moveBy(dx, dy); } } } function getCharLeft() { return domtool.getLayer("char0").getX(); } function getCharTop() { return domtool.getLayer("char0").getY(); } コード
すみません、HTMLも追加したかったのですが、文字数制限でダメだったので、URLを追記します。
http://up-denshi.candypop.jp/11/rpg8.cgi
上記ボタンダウンで、つまづいております。
ボタンを押した時に、上手くいかず困っています。
何位とぞ、よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー