• ベストアンサー

HTML・CSS ボタンの表示

<div class="item"> <tr> <td><input type="button" value="サンドウィッチ" onclick="get_calc(this)"></td> <td><input type="button" value="おにぎり" onclick="get_calc(this)"></td> <td><input type="button" value="お茶" onclick="get_calc(this)"></td> <td><input type="button" value="お弁当" onclick="get_calc(this)"></td> </tr> </div> ボタンを画面の50%の幅に均一の大きさで表示したです. CSSわかる方,教えてください!お願いします!

  • CSS
  • 回答数2
  • ありがとう数0

質問者が選んだベストアンサー

  • ベストアンサー
  • retorofan
  • ベストアンサー率33% (328/973)
回答No.2

<h2>CSS適用</h2> <pre> 手順は次のとおりです。 1 テーブルを親要素の半分の大きさにします。 2 テーブルの各セルがテーブルの幅の25%になるようにします。 3 ボタンがそのセルの幅いっぱいに広がるようにします。 </pre> <style> .item table { width: 50%; } .item td { width: 25%; } .item input[type="button"] { width: 100%; } </style> <div class="item"> <table> <tr> <td><input type="button" value="サンドウィッチ" onclick="get_calc(this)"></td> <td><input type="button" value="おにぎり" onclick="get_calc(this)"></td> <td><input type="button" value="お茶" onclick="get_calc(this)"></td> <td><input type="button" value="お弁当" onclick="get_calc(this)"></td> </tr> </table> </div>

その他の回答 (1)

  • t_ohta
  • ベストアンサー率38% (5072/13251)
回答No.1

<div class="item"> <table style="width:50%;table-layout:fixed;"> <tr> <td><input type="button" style="width:100%;" value="サンドウィッチ" onclick="get_calc(this)"></td> <td><input type="button" style="width:100%;" value="おにぎり" onclick="get_calc(this)"></td> <td><input type="button" style="width:100%;" value="お茶" onclick="get_calc(this)"></td> <td><input type="button" style="width:100%;" value="お弁当" onclick="get_calc(this)"></td> </tr> </table> </div>

関連するQ&A

  • javascriptで電卓を作成

    プログラミングをまったくやったことがない状態から、ドットインストールやテックアカデミーのHTML,CSS,Javascriptの入門動画をすべて見終わった段階ですので、 基本的な仕組みと関数やfor文、if文、while文、などについてくらいは理解しています。 今回、javascriptを使って電卓のシステムを組む、という課題があり、 ネットで検索してやってみてはいるのですが行き詰っています。 最初は以下のような形で作成をしていました。 <body> <form name="myform">  <table border="1"> <tr> <td colspan="4"> <input type="text" name="result" value="" id="result"> </td> </tr> <tr> <td><input type="button" value=" "onclick="calc(' ')" ></td> <td><input type="button" value="%"onclick="calc('%')"></td> <td colspan="2"> <input type="button" value="AC" onclick="calc('AC')"></td> </tr> <tr> <td><input type="button" value="7" onclick="calc('7')"></td> <td><input type="button" value="8" onclick="calc('8')"></td> <td><input type="button" value="9" onclick="calc('9')"></td> <td><input type="button" value="÷" onclick="calc('÷')"></td> </tr> <tr> <td><input type="button" value="4"onclick="calc('4')" ></td> <td><input type="button" value="5"onclick="calc('5')" ></td> <td><input type="button" value="6"onclick="calc('6')"></td> <td><input type="button" value="×" onclick="calc('×')"></td> </tr>   <tr> <td><input type="button" value="1" onclick="calc('1')" ></td> <td><input type="button" value="2" onclick="calc('2')"></td> <td><input type="button" value="3" onclick="calc('3')"></td> <td><input type="button" value="-" onclick="calc('-')"></td> </tr> <tr> <td><input type="button" value="0" onclick="calc('0')" ></td> <td><input type="button" value="+" onclick="calc('+')"></td> <td colspan="2"> <input type="button" value="=" onclick="calc('=')"></td> </tr> </form> <script> function calc(a) { if (a === "=") { document.myform.kekka.value = eval(document.myform.kekka.value); } else if (a === "AC") { document.myform.kekka.value = ""; } else { document.myform.kekka.value += a; } </script> </body> </html> ですが、onclickとevalを使わず、代わりに「addEventListner」を用いるよう指示されました。 「addEventListner」に関しては、検索してなんとかざっと理解したのですが、それ以前に、電卓の基本的な関数の組み方を理解していないため、いまいちどこから手をつけたらいいかわかりません。 いろんなサイトを見ているといろんなやり方がでてきますが、ope=+,やflag=0などの表記をよく見かけます。が、この意味もいまいちわかっていません。。 初心者すぎるので、何かアドバイスをいただけますと幸いです。 宜しくお願いいたします。 また、このような初心者が使いこなせるようになるために向いている動画や書籍、ページがありましたら教えていただけますと助かります。

  • テンキーで「0.1」を打つと「.1」になる

    HTMLページにテンキーを打つと数字が出るといったものをしたくて、検索しましたところ、参考サイトを見つけました。 https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q13197413368 「.」を追加したソースが下記になります。 「0.1」とテンキーを打つと「.1」になってしまいます。「0.1」になる方法を教えて下さいますか? 宜しくお願いします。 <style type="text/css">table { border: 0px; margin: 0 auto; border-collapse: collapse; } div#cc { text-align: center; } input { font-size: 12px; margin: 0; padding: 0; } input.key { width: 32px; font-family: monospace; font-size: 12px; } </style> </head> <body> <div id="cc"> <form name="myForm" action="#"> <input name="tx1" size=24 tabindex=1 maxlength=20 value="" onClick="selecttext(this)"><br> <input name="tx2" size=24 tabindex=2 maxlength=20 value="" onClick="selecttext(this)"><br> <input name="tx3" size=24 tabindex=3 maxlength=20 value="" onClick="selecttext(this)"><br> <hr> <table> <tr> <td><input type="button" class="key" value="7" onClick="keyEvent('7')"></td> <td><input type="button" class="key" value="8" onClick="keyEvent('8')"></td> <td><input type="button" class="key" value="9" onClick="keyEvent('9')"></td> </tr> <tr> <td><input type="button" class="key" value="4" onClick="keyEvent('4')"></td> <td><input type="button" class="key" value="5" onClick="keyEvent('5')"></td> <td><input type="button" class="key" value="6" onClick="keyEvent('6')"></td> </tr> <tr> <td><input type="button" class="key" value="1" onClick="keyEvent('1')"></td> <td><input type="button" class="key" value="2" onClick="keyEvent('2')"></td> <td><input type="button" class="key" value="3" onClick="keyEvent('3')"></td> </tr> <tr> <td><input type="button" class="key" value="0" onClick="keyEvent('0')"></td> <td><input type="button" class="key" value="." onClick="keyEvent('.')"></td> <td><input type="button" class="key" value="←" onclick="oneClear()"></td> <td><input type="button" class="key" value="CA" onclick="allClear()"></td> </tr> </table> </form> </div> <script type="text/javascript"> var t; selecttext(document.myForm.tx1); function selecttext(o) { t = o; } function keyEvent(c) { t.value = (t.value=='0')? c: t.value+c; } function allClear() { t.value = ''; } function oneClear() { t.value = t.value.substring(0,t.value.length-1); } </script> </body>

  • JavaScriptでIE上のボタンクリック

    JavaScriptの初心者でIE上で表示されたボタンをクリックしたいのですが、2つのボタンのうち1つをクリックしたいです。 「document.all("Buttonok").click();」,「document.getElementById("Buttonok").click();」と記入してみたのですが、エラーとなります。 下記のようなボタンをJavaScriptで押すにはどうしたらいいのでしょうか? どなたかすいませんがご教授ください。 ===================================================== ボタンのHTML ※formで囲まれてないです。 <table border="0" cellspacing="0" cellpadding="0" width="150"> <tr> <td width="50%"> <div id="Button"> <input type="buttonok" value="OK" class="Button" onclick="onClk("1");" /> </div> <div id="Button"> <input type="buttonng" value="NG" class="Button" onclick="onClk("0");" /> </div> </td> </tr> </table>

  • 電卓をつくったのですが

    <script type="text/javascript"> <!-- var to=0; var inp=""; var cal="+"; var flg=1; function df(a){ flg=0; inp+=a; document.ad1.z.value= inp; } function df1(a){ if(flg==0){ flg=1; var don=to+cal+inp; to=eval(don); inp=""; document.ad1.z.value=to; } if(a=="="){ to=0; cal="+"; }else{ cal=a; } } function df3(){ to=0; cal="+"; inp=""; document.ad1.z.value=to; } --> </script> </head> <body> <div id="s1"> <div id="s2"> <form name="ad1"> <table> <tr><td colspan="4">電卓</td></tr> <tr><td colspan="3"><input type="text" size="12px" name="z" value="0"></td> <tr> <td><input type="button" name="ad7" value="7" onClick="df(7)"></td> <td><input type="button" name="ad8" value="8" onClick="df(8)"></td> <td><input type="button" name="ad9" value="9" onClick="df(9)"></td> </tr> <tr> <td><input type="button" name="ad6" value="6" onClick="df(6)"></td> <td><input type="button" name="ad5" value="5" onClick="df(5)"></td> <td><input type="button" name="ad4" value="4" onClick="df(4)"></td> </tr> <tr> <td><input type="button" name="ad3" value="3" onClick="df(3)"></td> <td><input type="button" name="ad2" value="2" onClick="df(2)"></td> <td><input type="button" name="ad11" value="1" onClick="df(1)"></td> </tr> <tr> <td><input type="button" name="add1" value="+" onClick="df1(+)"></td> <td><input type="button" name="add2" value="-" onClick="df1(-)"></td> <td><input type="button" name="add3" value="×" onClick="df1(*)"></td> </tr> <tr> <td><input type="button" name="add4" value="÷" onClick="df1(/)"></td> <td><input type="button" name="add5" value="=" onClick="df1(=)"></td> <td><input type="button" name="add6" value="." onClick="df1(.)"></td> <td><input type="button" name="add7" value="c" onClick="df3()"></td> </tr> </table> </form> </div> </div> </body> </html> クリアーは上手くいったんですが 計算ができませんでした。 どこがいけないのでしょうか?

  • jQueryでテーブル行の表示・非表示

    JavaScript・jQueryでテーブル行(trタグ)の表示・非表示を 切り替えるスクリプトを以下のように作成しましたが うまく動きません。 dispChg()内で 非表示に設定したクラス名(hid)を追加し 全行を非表示にできていますが 次の該当trから 非表示に設定したクラス名(hid)の削除がうまくいかないようです。 JavaScriptの引数からクラス名を引き継げないのでしょうか。 どなたか修正できる方がいらっしゃいましたら 教えてください。 よろしくお願いいたします。 <html> <head> <title>test</title> <style type="text/css"> <!-- .hid { display:none } --> </style> <script src="jquery-1.2.6.pack.js" type="text/javascript"></script> <script type="text/javascript"> <!-- function dispChg(trClass){ /* まず全行を非表示 */ $('tr[class]').addClass('hid'); /* 該当の行を表示 */ $('tr[class*=trClass]').removeClass('hid'); } --> </script> </head> <body bgcolor=#ffffff topmargin="0" leftmargin="0" marginheight="0" marginwidth="0"> <input type="button" value="全部表示" onclick="dispChg('atype btype ctype dtype')"> <input type="button" value="a" onclick="dispChg('atype')"> <input type="button" value="b" onclick="dispChg('btype')"> <input type="button" value="c" onclick="dispChg('ctype')"> <input type="button" value="d" onclick="dispChg('dtype')"> <table border="1"> <tr class="atype"><td>a</td></tr> <tr class="btype"><td>b</td></tr> <tr class="ctype"><td>c</td></tr> <tr class="dtype"><td>d</td></tr> </table> </body> </html>

  • 「戻るボタン」 : ボタンを使わずに、・・・

    HTMLでは、戻るボタンは、 <FORM> <INPUT TYPE="button" VALUE="戻る" onClick="history.back </FORM> ですが、この動作で、 戻るボタンではなく、 <table><tr><td>戻る</td></tr></table> をクリックするようにしたいのですが、・・・ 可能でしょうか。 可能でしたら、htmlをお願いします。

    • ベストアンサー
    • HTML
  • ラジオボタンで選択した項目の合計金額を出したい

    簡単な見積フォームを作成中です。 ラジオボタンで商品を選択して、その合計金額を表示させたいのですが、何故か”undefined”と表示されてしまいます。 根本的な考え方が間違っているのでしょうか? 皆様ご教授の程お願いします。 <HTML><HEAD> <!-- function calc(){ hontai=document.ordersheet.hontai.VALUE; document.ordersheet.output.value=hontai; } //--> </SCRIPT></HEAD> <BODY> <FORM NAME="ordersheet" METHOD="POST"onsubmit="return check()"> <TABLE cellPadding=3 width=680> <TBODY><TR> <TD align=middle colSpan=5>お見積り内容 <TR><TD align=middle width=30> <TD align=middle width=200> <TD align=middle width=150>商品名 <TD align=middle width=150>商品名 <TD align=middle width=150>商品名 </TD> <TR><TD rowspan="2" align=middle width=30>本体 <TD rowspan="2" width=350></TD> <TD width=145>A商品<BR>273000円(税込)<BR> <TD width=145>B商品<BR>288750円(税込)<BR> <TD width=145>C商品<BR>109410円(税込)<BR> </TD> <TR> <TD><DIV><INPUT TYPE="radio" NAME="hontai" VALUE="273000" onClick="calc()"></DIV></TD> <TD><DIV><INPUT TYPE="radio" NAME="hontai" VALUE="288750" onClick="calc()"></DIV></TD> <TD><DIV><INPUT TYPE="radio" NAME="hontai" VALUE="109410" onClick="calc()"></DIV></TD> </TR> <TR><TD align=center colspan="2">合計金額</TD> <TD colspan="3" ><INPUT TYPE="text"NAME="output" SIZE="12">円</TD></TR> </TBODY></TABLE></FORM> </BODY></HTML>

  • javascript ボタンを非表示にしたい

    javascriptを学習しています。 3回目以降は追記できないようにしましたが、ボタンを非表示に出来ておりません。 どなたかご教授お願いいたします。 <!DOCTYPE html> <html> <head> <title>アカウント登録</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div class="base container"> <div class="row"> <h1>アカウント登録</h1> </div> <div class="row"> <div class="card" id="card"> <div class="card-block"> <form id="form-area" class="form-inline" method="post"> <div class="form-group"> <div class="input-group"> <input id="namae" name="namae" type="text" class="form-control" placeholder="名前"> </div> </div> <div class="form-group"> <div class="input-group"> <input id="nickname" name="nickname" type="text" class="form-control" placeholder="ニックネーム"> </div> </div> <div class="form-group"> <input id="tuikaBtn" type="button" name="touroku" value="追加"> </div> <table id="result-table"> <thead> <tr> <th>名前</th> <th>ニックネーム</th> <th>削除</th> </tr> </thead> </table> </form> <div id="tuikaMoto"> </div> <div id="result"></div> </div> </div> </div> </div> </div> <div id="overLay"> </div> <script> class NameTable { constructor() { this.data = []; this.table = document.getElementById('result-table'); this.tbody = this.table.createTBody(); this.btn = document.getElementById("tuikaBtn"); this.input_namae = document.getElementById("namae"); this.input_nickname = document.getElementById("nickname"); } handleEvent(e) { if (e.target === this.btn) { const name_value = this.input_namae.value, nickname_value = this.input_nickname.value; if (this.checkTsuika(name_value, nickname_value)) { this.pushData(name_value, nickname_value); this.createTable(); }; } else if (e.target.matches('.del')) { this.clickDel(e.target); } } pushData(name, nickname) { /* DBなどに追加する場合は、ここでfetchなどで通信 */ this.data.push({ name: name, nickname: nickname }); window.alert(`「[${name}]さん[${nickname}]にて登録しました。」`); this.input_namae.value = ''; this.input_nickname.value = ''; if (this.data.length > 2) { Object.assign(this.btn, { value: '', disabled: true, }); } } createTable() { while (this.tbody.firstChild) { this.tbody.removeChild(this.tbody.firstChild); } this.data.forEach(d => { const tr = this.tbody.insertRow(-1); tr.insertCell(0).appendChild(document.createTextNode(d.name)); tr.insertCell(1).appendChild(document.createTextNode(d.nickname)); const input_del = document.createElement('input'); Object.assign(input_del, { type: 'button', value: '削除', className: 'del', }); tr.insertCell(2).appendChild(input_del); }); } checkTsuika(name, nickname) { if (name === "" || nickname === "") { return false; } return window.confirm(`「[${name}]さん[${nickname}]を登録します。よろしいですか?」`); } clickDel(target) { /* fetchなどで削除を送信 */ const dels = [...this.tbody.querySelectorAll('.del')]; this.data = this.data.filter((_, i) => dels[i] !== target); this.createTable(); Object.assign(this.btn, { value: '追加ボタン', disabled: false, }); } } // 実行 window.addEventListener('DOMContentLoaded', () => { const table = new NameTable(); document.addEventListener('click', table, false); }); </script> </body> </html>

  • ラジオボタンが選択によるチェックボックス・ボタンの制御方法

    お世話になります。 やりたい事 (1)ラジオボタン【小学校】:小学校のみ入力可 (2)ラジオボタン【中学校】:中学校(市立・私立)のみ入力可 (3)ラジオボタン【全ての学校】:小中すべて入力可 (1)と(2)は動作していると思います。 (3)を実現するにはどうすればいいのでしょうか? よろしくお願いします。 <html> <head> <title>ラジオボタンが選択されたらチェックボックス・ボタンを入力可能にする</title> <script Language="JavaScript"><!-- function setRTF(f,n){ for(var i=0; i<f.length; i++){ var p=f[i].parentNode; var pid=""; while(p){ if(p.nodeName=="TD"){pid=p.id;break;} p=p.parentNode; } if(f[i].type=="checkbox" || f[i].type=="button") f[i].disabled=pid!=n; } } function allcheck(obj,flag){ var target=obj.parentNode.getElementsByTagName("input"); for(var i=0;i<target.length;i++){ if(target[i].type=="checkbox") target[i].checked=flag; } } // --></script> </head> <body> <form> <table border="1" width="303"> <tr> <td width="89"><input type="radio" name="mm" onClick="setRTF(this.form,'aa')">小学校</td> <td width="90"><input type="radio" name="mm" onClick="setRTF(this.form,'bb')">中学校</td> <td width="102"><input type="radio" name="mm" onClick="setRTF(this.form,'xx')">全ての学校</td> </tr> <tr> <td id="aa" rowspan="2" width="89"> <input type="checkbox" name="aa[]" value="1" disabled>A小学校<br> <input type="checkbox" name="aa[]" value="2" disabled>B小学校<br> <input type="checkbox" name="aa[]" value="3" disabled>C小学校<br> <input type="button" value="全選択" onClick="allcheck(this,true)"/ disabled ><br> <input type="button" value="全解除" onClick="allcheck(this,false)" disabled /> </td> <td id="bb" width="90"> 市立<br> <input type="checkbox" name="bb[]" value="1" disabled>A中学校<br> <input type="checkbox" name="bb[]" value="2" disabled>B中学校<br> <input type="checkbox" name="bb[]" value="3" disabled>C中学校<br> <input type="button" value="全選択" onClick="allcheck(this,true)" disabled /><br> <input type="button" value="全解除" onClick="allcheck(this,false)" disabled /> </td> <td id="cc" rowspan="2" width="102"> </td> </tr> <tr> <td id="bb" width="90"> 私立<br> <input type="checkbox" name="cc[]" value="1" disabled>A中学校<br> <input type="checkbox" name="cc[]" value="2" disabled>B中学校<br> <input type="checkbox" name="cc[]" value="3" disabled>C中学校<br> <input type="button" value="全選択" onClick="allcheck(this,true)" disabled /><br> <input type="button" value="全解除" onClick="allcheck(this,false)" disabled /> </td> </tr> </table> </form> </body> </html>

  • jqueryの置き換えについて

    jsで作った電卓をjqueryに置き換えしなくてはいけなくなったのですが、 jqueryは習ってる最中で何がなんやら書き換えがうまくできません。 jqueryの導入の仕方や基本的な書き方はわかるのですが、書き換えると何かしら必ず定義されていないなどエラーが出てしまいます。jsで書いたものはちゃんと動いていたのにjqueryに書き換えると微動だにしません。 書き換え後のコードを頂けたら幸いです。 html <body class="body1"> <div class="calculator"> <div id="display" value="0">0</div> <div class="commands"> <div class="command is-2col" onclick="calc_func(this)" value="clear">clear</div> <div class="command empty"></div> <div class="command is-operator" onclick="calc_func(this)" value="/">÷</div> <div class="command" onclick="calc_func(this)" value="7">7</div> <div class="command" onclick="calc_func(this)" value="8">8</div> <div class="command" onclick="calc_func(this)" value="9">9</div> <div class="command is-operator" onclick="calc_func(this)" value="*">×</div> <div class="command" onclick="calc_func(this)" value="4">4</div> <div class="command" onclick="calc_func(this)" value="5">5</div> <div class="command" onclick="calc_func(this)" value="6">6</div> <div class="command is-operator" onclick="calc_func(this)" value="-">-</div> <div class="command" onclick="calc_func(this)" value="1">1</div> <div class="command" onclick="calc_func(this)" value="2">2</div> <div class="command" onclick="calc_func(this)" value="3">3</div> <div class="command is-operator" onclick="calc_func(this)" value="+">+</div> <div class="command is-2col" onclick="calc_func(this)" value="00">00</div> <div class="command" onclick="calc_func(this)" value="0">0</div> <div class="command is-equal" onclick="calc_func(this)" value="=">=</div> </div> </div> <body> js function calc_func(button) { // console.log(button); var display = document.getElementById("display");//id名 displayの要素を取得 変数displayに代入 var button_value = button.innerHTML; //button_valueの変数に、buttonの中身を取得 console.log(button_value) if (button_value === "clear") { //押したのがclearなら、displayの中身がinnerHTMLで0に変わる display.innerHTML = "0"; }else if (button_value === "=") { //押したのが=なら、displayの中身をnew funcで計算して値を返す var replace_value = display.innerHTML.replaceAll("×", "*").replaceAll("÷", "/"); //replaceAllは("対象の文字列","置き換えする文字列")で文字を置き換えできる display.innerHTML = (new Function ("return " + replace_value))(); //押されたキーの内容を判別せず、単純にそのまま文字列として記憶して、計算時に、そのまま文字列の計算結果を返すようにreturnの文字列を付け加える }else if (display.innerHTML === "0" || display.innerHTML === "00") { if (!isNaN(button_value) || button_value === "-") { display.innerHTML = button_value; } }else { if (isNaN(button_value)) { var migihashi = display.innerHTML.slice(-1); if (!isNaN(migihashi)) { display.innerHTML += button_value; } }else { display.innerHTML += button_value; //それ以外の動き +=はdisplay.innerHTML=display.innerHTML+button_valueの略し } } } css .calculator { width: 205px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5); margin: auto; } #display { width: 205px; background: #5b5b5b; text-align: right; border: 1px solid #000; box-sizing: border-box; color: #fff; font-size: 25px; padding: 5px 10px; } .display.flashing { animation-name: flash; animation-duration: 0.1s; } @keyframes flash { 0% { color: #5b5b5b; } 90% { color: #5b5b5b; } 100% { color: #fff; } } .commands { width: 204px; background: #000; display: flex; flex-wrap: wrap; padding-left: 1px; } .command { width: 50px; height: 45px; background: #ddd; margin: 0 1px 1px 0; text-align: center; padding-top: 10px; box-sizing: border-box; font-size: 18px; } .command.is-operator, .command.is-equal { background: orange; color: #fff; } .command.is-2col { width: 101px; } .command:active { background: #bbb; } .command.is-operator:active, .command.is-equal:active { background: #d37e00; } .command.empty:active { background: #ddd; } #debug { display: none; }

専門家に質問してみよう