人力検索はてな
モバイル版を表示しています。PC版はこちら
i-mobile

javascriptの質問です
以下のjavascriptのhoge1を実行すると
テーブルの1行目が消えます
hoge1を実行した後hoge2を実行すると1行目も2行目も両方消えてしまいます
hoge1を実行した後hoge2を実行しても2行目だけ消えて欲しいのですが
その場合hoge2に今までのjavascriptを無効化するコードを追加しないといけないと思うのですが
無効化するコードを教えて下さい

function hoge1() {
var c = document.querySelectorAll("td:nth-child(1)");
for (var d = 0; d < c.length; d++) {
c[d].style.display = "none"
}
};
function hoge2() {
var c = document.querySelectorAll("td:nth-child(2)");
for (var d = 0; d < c.length; d++) {
c[d].style.display = "none"
}
};

●質問者: takanii
●カテゴリ:ウェブ制作
○ 状態 :終了
└ 回答数 : 1/1件

▽最新の回答へ

1 ● 河津武志
●300ポイント

すでにrouge_2008さんがコメントされていて、私もそのとおりだと思いますが、スタイルシートのルールを直接書き換えるという方法もあります。

<style>
/* これが0番目のstyle要素だとする。 */
td:nth-child(1) { display: table-cell; }
td:nth-child(2) { display: table-cell; }
</style>
<script>
function hoge1() {
var rules = document.getElementsByTagName('style')[0].sheet.cssRules;
rules[0].style.cssText = 'display: none';
rules[1].style.cssText = 'display: table-cell';
}
function hoge2() {
var rules = document.getElementsByTagName('style')[0].sheet.cssRules;
rules[0].style.cssText = 'display: table-cell';
rules[1].style.cssText = 'display: none';
}
</script>

関連質問

●質問をもっと探す●



0.人力検索はてなトップ
8.このページを友達に紹介
9.このページの先頭へ
対応機種一覧
お問い合わせ
ヘルプ/お知らせ
ログイン
無料ユーザー登録
はてなトップ