質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

2回答

2087閲覧

google chrome の上画面の「←」ボタンを押してキャラクタを移動したい

uepon

総合スコア17

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2018/06/25 05:13

編集2018/06/25 06:17

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

上記ボタンダウンで、つまづいております。
ボタンを押した時に、上手くいかず困っています。
何位とぞ、よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maisumakun

2018/06/25 05:15

具体的に、どのような問題が起きているのでしょうか。
m.ts10806

2018/06/25 05:23

「google chrome のdocument.getElementById」とは何を指しているのでしょうか?提示のコードがもともと何を目的として組まれたものなのか、どのような問題が起きているのか記載してください。
uepon

2018/06/25 05:25

chromeでデバッグしており、if(document.getElementById)の直後にブレークポイントを貼っているのですが、マウスでボタンをクリックしてもここにブレークしません。よろしくお願いいたします。
uepon

2018/06/25 05:32

mts10806様、初期化の時に、ここに飛んで来て、画面中のマウスでボタンをクリックしたらここに飛んでくると思っています。よろしくお願いいたします。
m.ts10806

2018/06/25 05:34

説明不足部分と実際のコードがこれだけでは足りないと思います。html等もあるのではないでしょうか。回答者は再現確認を行ってから回答する人も多いです。なるべく再現確認が可能な状態にしてください。質問は編集することができます。なるべく質問テンプレートにそって書かれた方が要件が伝わりやすくなり、回答も得やすくなります。 https://teratail.com/help/question-tips#questionTips3-5
uepon

2018/06/25 05:35

全コードを追加しました。よろしくお願いいたします。
m.ts10806

2018/06/25 05:36

htmlないのでしょうか?あと「全コード」が全コードになっていません。(冒頭function DomToolkit() {が抜けています)調整をお願いします。
m.ts10806

2018/06/25 05:38

コードブロックの冒頭を ```JavaScript など言語名を入れることで言語にあったハイライトが最適にされるので調整してください。※JavaScriptはjsと省略可能です。 https://teratail.com/questions/7119
m.ts10806

2018/06/25 05:39

JavaScriptのコードは1つにまとめてもらった方が良いですね。幾つかわけられたみたいですが、見ただけでは関係性が分かりません。
maisumakun

2018/06/25 05:41

コンソールにエラーメッセージなどは出ていませんか?
uepon

2018/06/25 05:42

mts10806様、冒頭function DomToolkit() {が抜けています)修正しました。
uepon

2018/06/25 05:45

mts10806様、コードをまとめました。maisumakun様、エラーメッセージは、出ていません。よろしくお願いいたします。
m.ts10806

2018/06/25 06:04 編集

修正前のコードに「OnButtonClick1()」というのがあったように思うのですが、消しました?またhtmlなしなんですか?既に指摘させていただいている通り、このJavaScriptのコードだけでは何の再現確認もとれません(再現確認がとれないので回答が得られないと思っていただいて差し支えありません)。どこから実行されているコードなのでしょうか。html、CSSもあわせてご提示ください。
m.ts10806

2018/06/25 06:06

読み飛ばしておりました。失礼しました。 >文字数制限でダメだった ところで、問題が発生するのはどの場面でしょうか。ボタンが沢山あるので「再現する手順」も具体的に記載してください。
uepon

2018/06/25 06:09

上画面中の左から2番目ボタンを押した時にキャラクターを移動させたいと考えています。
m.ts10806

2018/06/25 06:12

なるほど、では「google chrome のdocument.getElementByIdを取得したい」という質問内容を修正してください。実際の要件と書いてある内容が異なります。また、「左から2番目」ではなく「●●ボタン」のようにきちんと明示してください。
m.ts10806

2018/06/25 06:13

「↑」ボタンクリックが拾えているので同じようにすれば拾えそうですが、そうでもないのでしょうか。
uepon

2018/06/25 06:18

「↑」ボタンは、試験的にjavascriptをいれました。何卒よろしくお願いいたします。
uepon

2018/06/25 06:23

<input type="button" id="button_left" value="←" style="WIDTH: 100px; HEIGHT: 50px" name="left" >を<form name="cursor">でくくっているのんですが、javascriptの else if(document.getElementById) { // Mozilla document.addEventListener("keypress", handleKeypress, true); with(parent.input) { document.cursor.left.addEventListener("mousedown", handleMouseDown, true);に上手く拾ってくれないのです。
uepon

2018/06/25 06:30

kei344様 その入力画面から名前とキャラクタを選択後、参加するボタンを押してもらうと、問題の箇所に当たります。よろしくお願いいたします。
guest

回答2

0

直接の回答ではありませんが、// NN4というコメントがあるということは20年近く前のブラウザを対象にしたコードだと思われます。別途新しいコードを探されることをお勧めします。

【Netscape Navigator (ネットスケープコミュニケーションズ) - Wikipedia】
https://ja.wikipedia.org/wiki/Netscape_Navigator_(ネットスケープコミュニケーションズ)

Netscape Navigator 4.08 – 1998年11月9日 (16bit版Windowsと68k Macintoshの最終リリース)

投稿2018/06/25 06:44

kei344

総合スコア69398

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

下記プロパティはChromeでは非推奨となっていますが、プロパティ自体は参照可能なようです。

javascript

1document.all

MDN web doc

javascript

1if (document.all) { 2 // Chromeでもこちらに入る 3} else if (document.getElementById()) { 4} else { 5}

ですので、document.allのプロパティ有無を確認する分岐でIEのユーザエージェントも一緒に条件として入力してあげるのはいかがでしょうか。

javascript

1if (document.all && ユーザエージェントがIE) { 2} else if (document.getElementById()) { 3} else { 4}

投稿2018/06/25 06:22

yuusuko

総合スコア145

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

uepon

2018/06/25 06:34

回答ありがとうございます。今から試してみます。
yuusuko

2018/06/25 06:41 編集

maisumakun様> アドバイスありがとうございます。 ドキュメント確認いたしました。 そうなのですね、先走った回答をしてしまいました。
uepon

2018/06/25 06:51

/*if (document.all) { // Chromeでもこちらに入る } else*/ if(document.getElementById()) { // Mozilla document.addEventListener("keypress", handleKeypress, true); に変更したのですが、上手くいきません。 アドバイスの程、よろしくお願いいたします。
uepon

2018/06/25 06:53

document.addEventListener("keypress", handleKeypress, true);にブレークポイントを持ってきているのですが、「←」ボタンを押してもブレークされません。。 maisumakun様>アドバイスありがとうございます。
yuusuko

2018/06/25 06:59

initEventHandlerには入ってきていますでしょうか? 入ってきている場合、if (document.all)~の条件分岐のelseに入っているということでしょうか?
uepon

2018/06/25 07:05

if(document.getElementById()) {を document.getElementsByName()に変更してもダメでした。 element.getElementsByName()に変更してもダメでした。 アドバイスの程、よろしくお願いいたします。
uepon

2018/06/25 07:08

すみません。initEventHandlerにも入ってきていません。
yuusuko

2018/06/25 07:12 編集

initEventHandlerはinitファンションの実行時に実行されるようですが、initファンクションは実行されていますでしょうか?
uepon

2018/06/25 07:16

yuusuko様> お忙しい所、すみません。リロードすると、initファンクションは実行されています。
yuusuko

2018/06/25 07:23

setTimeoutでinitEventHandlerが呼ばれていないということですね。 試していなくて申し訳ないのですが、下記のようにsetTimeoutの第一引数を修正した場合、動作しますでしょうか? setTimeout("initEventHandler()", 100); ↓ setTimeout(initEventHandler, 100 );
uepon

2018/06/25 07:26

ありがとうございます。早速試してみます。今後とも宜しくお願いたします
uepon

2018/06/25 07:37

setTimeout("initEventHandler()", 100); ↓ 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(element.getElementsByName()) { どこのブレークポイントにも引っかかりません。 アドバイスの程、よろしくお願いいたします。
yuusuko

2018/06/25 07:43

ご掲示のjavascriptをコピーして、chromeのデベロッパーツールで実行したところ、initEventHandler()の「★ブレークポイント if(!parent.input」でブレークされました。 しつこいようで申し訳ありませんが、initファンクションは実行されていて、「setTimeout("initEventHandler()", 100);」の行も実行されているか確認いただけますでしょうか?
uepon

2018/06/25 07:52

質問ですみません。 setTimeout("initEventHandler()", 100); ↓ setTimeout(initEventHandler, 100 );は、どのようになっていますか? 申し訳ありませんが、教えて下さい。
yuusuko

2018/06/25 07:59 編集

setTimeoutの第一引数は「code」もしくは「function」となっています。 「code」の場合は、ご掲示のコードのように文字列で実行処理を記載します。 「function」の場合は、実行するファンクションをそのまま指定します。 指定した処理が最終的に実行されるという結果は同じですが、setTimeoutの中で行われていることは少し違います。 https://developer.mozilla.org/ja/docs/Web/API/WindowTimers/setTimeout
uepon

2018/06/25 08:07

yuusuko様 色々、ご教授ありがとうございます。 こちらでも、、chromeのデベロッパーツールで実行したところ、initEventHandler()の「★ブレークポイント if(!parent.input」でブレークされました。 ありがとうございます。 但し、「←」ボタンを押してもキャラクタが移動しません。 これは、何故ですか? ご教授の程、よろしくお願いいたします。
uepon

2018/06/25 08:14

if(!parent.input || !parent.input.document || !parent.input.document.cursor || !parent.input.document.cursor.down) { setTimeout("initEventHandler()", 100); return; } ここを少し、教えて頂けないでしょうか?
yuusuko

2018/06/25 08:14

「if(!parent.input~」で再帰的に「initEventHandler」を呼び出していますが、このif文の条件を回避できず、無限ループになっていませんでしょうか?
uepon

2018/06/25 08:17

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); } 出来ましたら、ここもご教授の程、よろしくお願いいたします。
uepon

2018/06/25 08:19

誠に無知でスミマセン。 parent.input~ を、教えて頂けないでしょうか、よろしくお願いいたします。
uepon

2018/06/25 08:20

確かに無限ループになっています。。。。。
yuusuko

2018/06/25 08:25

「parent」はウィンドウもしくは、フレームを使っている場合は親ウィンドウということになります。 ですので、「parent.input」はウィンドウか親ウィンドウにある「input」を指していること言うことになります。 https://developer.mozilla.org/ja/docs/Web/API/Window/parent
uepon

2018/06/25 08:41

ご助言ありがとうございます。 今、<iframe> で2つのウィンドウを指しているのですが、出来れば、可能なら一つのウィンドウにまとめたいと考えております。 今、上の「←」ボタンを押して、キャラクタをうごかしたいのですが、 if(!parent.input || !parent.input.document || !parent.input.document.cursor || !parent.input.document.cursor.down) { setTimeout("initEventHandler()", 100); return; } ↓ if(!input || !input.document || !input.document.cursor || !input.document.cursor.down) { setTimeout("initEventHandler()", 100); return; } と、変更したのですが、 if(!inputのinputでエラーが出ます。 どの様に、書けばいいのか、アドバイスの程、よろしくお願いいたします。
yuusuko

2018/06/25 08:47 編集

エラーが出ているのはinputが存在しないからです。 ページ構成を確認して、それにあわせた実装が必要となります。
uepon

2018/06/25 08:57

調べたのですが、ページ構成が解りません。<form id="hogehoge" name"yasuyasu"> if(!hogehoge || !hogehoge.document || !hogehoge.document.cursor || !hogehoge.document.cursor.down) { setTimeout("initEventHandler()", 100); return; } と、入れればいいのでしょうか?ご教授の程、よろしくお願いいたします。
yuusuko

2018/06/25 09:06

<form id="hogehoge"> であれば、「parent.hogehoge」で取得できるかと思います。 ですので、このエレメントの存在確認でしたら、 if(!parent.hogehoge~ となります。 (ページ構成が判然としませんので、これで取得できない場合もあることはご留意ください。)
uepon

2018/06/25 10:10 編集

if(!parent.main || !parent.main.document || !parent.main.document.cursor || !parent.main.document.cursor.down) { setTimeout("initEventHandler()", 100); return; }で、無限ループから、回避できました。ありがとうございます。 cgiにて、 if($FORM{iframe} eq 'main') { # ゲームフレーム printGameFrame(); } としていたので、こちらが効き、無限ループから、抜け出しました。 但し、 if(document.getElementById) { // if(element.getElementsByName()) { // Mozilla document.addEventListener("keypress", handleKeypress, true); with(parent.main) { // with(parent.cursor) { ★ブレークポイント 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); } ブレークポイント位置で「←」ボタンを押し続けるとリロード時にブレークがかかるのですが、キャラクターが動きません。 デバッグのため、ウィンドウズのポインタを動かしているためか、飛び先のhandleMouseDownに行くことが出来ません。 「←」ボタンを押すと、キャラクタを移動したいのですが、アドバイス頂けないでしょうか?
yuusuko

2018/06/25 10:22

ブレークで止まっているところはあくまで、マウスダウンのイベントを設定している場所です。 ですので、初期化時にしか止まらなくなっています。 実際にマウスダウンで動く処理はhandleMouseDownです。 「parent.input」の無限ループの時もそうでしたが、一つ一つ処理を見ていけば、なぜ目的の場所に行かないのかわかると思います。 目的の場所に来ないなどの時は、どういった処理でそこに来るのかをひとつづつ確認してみましょう。 地道にやっていくのが上達への早道です。
uepon

2018/06/25 11:22

重ね重ねご教授頂きありがとうございます。 お忙しい所、すみません。 handleMouseDown内のdocument.allをコメントアウトし、 // if(document.all) { // button = parent.input.event.srcElement.name; // } // else { button = event.target.name; // } handleKeypress内のdocument.allをコメントアウトしたのですが、 // if(document.all) { // key = window.event.keyCode; // } // else { key = event.which; // } moveChar(String.fromCharCode(key)); まだ、「←」ボタンを押しても、キャラクタが動きません。 handleMouseDown内の button = event.target.name;で★ブレークポイントを貼っているのですが、ブレークしません。 見当違いの事をしているのでしょうか? ご教授のほど、宜しくおねがいします。
yuusuko

2018/06/25 14:01

当初の質問内容からずれてきていると思っていましたが、タイトルが変更されていますね。 とりあえず、想定しているエレメントに対して、イベントがちゃんと追加できているかを確認してください。 parent.inputのように存在しないエレメントに対してイベントを追加していないでしょうか。 処理系にある一つ一つのものを確認して想定にそぐわない参照がされていないかも確認していってください。
uepon

2018/06/27 05:11

遅い時間に返信して頂き、ありがとうございます。現在、何とか怪しい所をデバック中、確認中です。また、詰まりましたら、質問させて頂きたいと思っています。もうしばらく、お待ち下さい。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問