function prepareEventHandlers() {
var sectionButton1 = document.getElementById("sectionButton1");
var sectionButton2 = document.getElementById("sectionButton2");
var sectionButton3 = document.getElementById("sectionButton3");
var sectionButton4 = document.getElementById("sectionButton4");
var sectionButton5 = document.getElementById("sectionButton5");
var enabled1 = true;
var enabled2 = false;
var enabled3 = false;
var enabled4 = false;
var enabled5 = false;
function checkEnabled() {
if (enabled1) {
sectionButton1.setAttribute("class", "sectionButtonEnabled");
}
if (enabled2) {
sectionButton2.setAttribute("class", "sectionButtonEnabled");
}
if (enabled3) {
sectionButton3.setAttribute("class", "sectionButtonEnabled");
}
if (enabled4) {
sectionButton4.setAttribute("class", "sectionButtonEnabled");
}
if (enabled5) {
sectionButton5.setAttribute("class", "sectionButtonEnabled");
}
}
checkEnabled();
sectionButton1.onmouseover = function() {
if (enabled1) {
sectionButton1.setAttribute("class", "sectionButtonOver");
}
};
sectionButton1.onmouseout = function() {
if (enabled1) {
sectionButton1.setAttribute("class", "sectionButtonEnabled");
}
};
sectionButton2.onmouseover = function() {
if (enabled2) {
sectionButton2.setAttribute("class", "sectionButtonOver");
}
};
sectionButton2.onmouseout = function() {
if (enabled2) {
sectionButton2.setAttribute("class", "sectionButtonEnabled");
}
};
sectionButton3.onmouseover = function() {
if (enabled3) {
sectionButton3.setAttribute("class", "sectionButtonOver");
}
};
sectionButton3.onmouseout = function() {
if (enabled3) {
sectionButton3.setAttribute("class", "sectionButtonEnabled");
}
};
sectionButton4.onmouseover = function() {
if (enabled4) {
sectionButton4.setAttribute("class", "sectionButtonOver");
}
};
sectionButton4.onmouseout = function() {
if (enabled4) {
sectionButton4.setAttribute("class", "sectionButtonEnabled");
}
};
sectionButton5.onmouseover = function() {
if (enabled5) {
sectionButton5.setAttribute("class", "sectionButtonOver");
}
};
sectionButton5.onmouseout = function() {
if (enabled5) {
sectionButton5.setAttribute("class", "sectionButtonEnabled");
}
};
}
window.onload = function() {
prepareEventHandlers();
};
答えて
いつでもあなた自身が「foo1の」、「foo2は」のような変数名を書く見つけ、あなたのイベントを追加するaddEventListenerを使用して検討する必要があり、それらはすべて同じことを多かれ少なかれ実行します。本当に配列を停止し、バックアップし、宣言する必要があります。今
function prepareEventHandlers() {
var sectionButtons = [];
for (var i = 1; i <= 5; ++i)
sectionButtons[i] = document.getElementById('sectionButton' + i);
var enabled = [ true, false, false, false, false ];
function checkEnabled() {
for (var i = 1; i <= 5; ++i)
if (enabled[i]) sectionButtons[i].className = 'sectionButtonEnabled';
}
checkEnabled();
for (i = 1; i <= 5; ++i) {
sectionButton[i].onmouseover = function(i) {
return function() {
if (enabled[i]) sectionButton[i].className = 'sectionButtonOver');
}
}(i);
sectionButton[i].onmouseout = function(i) {
return function() {
if (enabled[i]) sectionButton[i].className = 'sectionButtonEnabled';
}(i);
}
}
window.onload = function() {
prepareEventHandlers();
};
、他の二つの事柄:
- は "のsetAttribute()" と "class" 属性を設定しないでください。代わりに、DOM要素の "className"プロパティを操作します。
クラスを直接これらの文字列に設定するのではなく、独自の "addClass()"および "removeClass()"関数を作成することをお勧めします。クラスは、クラス名のリストで、スペースで区切ることができます。
function addClass(elem, c) { elem.className += ' ' + c; } function removeClass(elem, c) { elem.className = elem.className.replace(new RegExp('\\b' + c + '\\b', 'g'), '')); }
@antisanityよくフォームが送信されるとき、サーバー側からは良い考えではないかもしれません。しかし、それらをすべてグループ化するためにクラスを使用することはできます。 – Pointy
いいえ、「名前」属性は、フォームの投稿時にサーバーに表示される属性です。サーバー上で名前が異なると、「送信」ボタンがクリックされたことを示すことができます。もちろん、HTMLの外観はわかりません。 – Pointy
+1有効なクラスには単語境界文字を含めることができるので、正規表現で ''\\ b''を使用しません。 (この場合、もちろん害はありません。) – RightSaidFred
EDIT
私は配列にデータを格納話して他の回答の多くに同意するが、代わりに、並列配列で、私は、オブジェクトの配列使用します。
var i, buttonData = [];
for(i = 1; i <= 5; i++)
buttonData.push({ "enabled" : false,
"button": document.getElementById("sectionButton" + i) });
buttonData[0].enabled = true;
を
そして:
for (i = 0; i < buttonData.length; i++) {
setClassIfEnabled(buttonData[i].enabled, buttonData[i].button)
}
それともしたい場合それをシンプルに保つため、以下のオリジナルの答えはまだあなたの元のバージョンのうちの多くのコードをチョップします:
リファクタリングは、ヘルパーメソッドにコードを重複
function setClassIfEnabled(enabled, button){
if (enabled) {
button.setAttribute("class", "sectionButtonEnabled");
}
}
そして
function checkEnabled() {
setClassIfEnabled(enabled1, sectionButton1);
setClassIfEnabled(enabled2, sectionButton2);
setClassIfEnabled(enabled3, sectionButton3);
setClassIfEnabled(enabled4, sectionButton4);
setClassIfEnabled(enabled5, sectionButton5);
}
また
function setMouseOverIfEnabled(enabled, button) {
button.onmouseover = function() {
if (enabled) {
button.setAttribute("class", "sectionButtonEnabled");
}
};
}
setMouseOverIfEnabled(enabled1, sectionButton1);
setMouseOverIfEnabled(enabled2, sectionButton2);
setMouseOverIfEnabled(enabled3, sectionButton3);
setMouseOverIfEnabled(enabled4, sectionButton4);
setMouseOverIfEnabled(enabled5, sectionButton5);
そしてもちろん。またmouseout
のために同じことを行う、あなたは
function setMouseOverIfEnabled(enabled, button) {
button.addEventListener("mouseover", function() {
if (enabled) {
button.setAttribute("class", "sectionButtonEnabled");
}
});
}
もちろん、文全体を5回繰り返すのではなく、1から5までの数値で反復処理するほうが、コードが少なくて済みます。 – Pointy
@Pointy - 確かに、有効な変数を反復処理する必要があります。おそらく配列に格納することで可能です。私はOPのためにそれを簡単に保ちたいと思っていました。 "そして、シャーリーと呼ぶのをやめる" :-) –
@Pointy - コメントをいただき、ありがとうございます。私はあなたの答えをコピーしたとは思わないでください –
ここに凝縮されたバージョンがあります:このような機能は、次のようになります。異なる変数だけでコードのブロックを繰り返すことは絶対にありません。ループを使用するか、ローカル関数を作成します。あなたのIDは、シーケンシャルなので、ループはここでうまく動作します。この場合、:
function prepareEventHandlers()
{
var button;
var enabled = [true, false, false, false, false];
for (var i = 0; i < enabled.length; i++) {
button = document.getElementById("sectionButton" + (i + 1));
button.buttonEnabled = enabled[i];
if (button.buttonEnabled) {
button.className = "sectionButtonEnabled";
}
button.onmouseover = function() {
if (this.buttonEnabled) {
this.className = "sectionButtonOver";
}
}
button.onmouseout = function() {
if (this.buttonEnabled) {
this.className = "sectionButtonEnabled";
}
}
}
}
また、このコードは、ボタンのbuttonEnabled
財産および/またはクラス名を操作して、後で他のコードでボタンを有効にすることができますし、イベントハンドラは自動的に正しいことを行います。
最初に:オンスタイルのボタンにonmouseoverとonmouseoutを使用するのは、1990年代のものです。あなたは今、CSSで同じことをすることができます。
.sectionButtonEnabled { regular styles here }
.sectionButtonEnabled:hover { mouseover styles here }
(注、IEのために、これは「標準モード」が必要 - 読み:DOCTYPEラインを持っている - 以降およびIE7または)今
、あなたは本当に物事に古いを行いたい場合壊れてしまいます...
function prepareEventHandlers() {
var buttons = [
"sectionButton1",
"sectionButton2",
"sectionButton3",
"sectionButton4",
"sectionButton5"
];
var enabled = [ true, false, false, false, false ];
for (var i = 0; i < buttons.length; ++i) {
var elem = document.getElementById(buttons[i]);
if (enabled[i]) {
elem.className = "sectionButtonEnabled";
// Since you're only attaching the event handler to enabled buttons,
// you already know that `enabled` is true. So you don't even need to
// check, since there's no way to change your local variable.
elem.onmouseover = function() {
this.className="sectionButtonOver";
};
elem.onmouseout = function() {
this.className="sectionButtonEnabled";
};
}
}
}
マウスオーバーハンドラーが必要ない場合は、それらを取り除き、上記のCSSを使用してください。
- 1. このJavascript/jQueryアニメーションをより良くするにはどうすればよいですか?
- 2. これはどのように短く書くのですか?
- 3. このコードをより短くするにはどうすればよいでしょうか?
- 4. このコードをより効率的に短くするにはどうすればよいですか?
- 5. どうすれば短くて良いのですか?
- 6. jsでURLを短くするにはどうすればよいですか?
- 7. フレームワークを組み込むことなくフレームワークを構築するにはどうすればよいですか?
- 8. Java Androidコードを短くするにはどうすればよいですか?
- 9. このデータベースをより良くするにはどうすればよいですか?
- 10. このデータベーススキーマをより良くするにはどうすればよいですか?
- 11. この連絡先をより良くするにはどうすればよいですか?
- 12. 多くのチェックボックスでコードを短くするにはどうすればよいですか?
- 13. 以下のJavaコードをより良く書くにはどうすればよいですか?
- 14. このコードをJSで短くするにはどうすればよいですか?
- 15. これらの計算操作を短くするにはどうすればよいですか(Java)
- 16. スクロールバーを何も表示しない - ページを短くするにはどうすればよいですか?
- 17. このコードを繰り返しにくくするにはどうすればよいですか?ランダムパスワードジェネレータ
- 18. このSQLを短縮するにはどうすればよいですか?
- 19. このコードを短縮するにはどうすればよいですか?
- 20. 多くのif文を短くするにはどうすればよいですか? (Python 3.x)
- 21. このエラーを取り除くにはどうすればよいですか?
- 22. このデッドロックを取り除くにはどうすればよいですか?
- 23. このエラーを取り除くにはどうすればよいですか?
- 24. このセグメンテーションエラーを取り除くにはどうすればよいですか?
- 25. 読み書きでこれをより速くするにはどうすればよいですか?
- 26. 私のpalidromプログラムをより良くするにはどうすればいいですか?
- 27. この複雑なクエリをより速く実行させるにはどうすればよいですか?
- 28. 新しいウィンドウを開くことなくウェブページを開くにはどうすればよいですか?
- 29. 文字列を短くするにはどうすればいいですか?
- 30. これをより動的な方法で書くにはどうすればよいですか?
これは、作成されたフレームワークの一部です。なぜフレームワークを使いたくないのですか? –
これはクラスの一部なので、フレームワークを使用するとカウントされません。 – 0x499602D2
視覚効果が必要な場合は、例えばJavascriptではなくCSSを使用する必要があります。 –