扫码关注官方订阅号
我想用javascript写个网页上的10×10的clickable grid。通过点击每个cell,可以在上面输入1-9的数字。 我昨天在google上搜了很久都没有找到相关的代码,作为新手自己也实在写不出来,特来此求教,希望大家帮帮我吧。灰常感谢
没怎么写过js,刚好拿来玩玩。我的chrome 34 (mac 10.9) 上通过。能用,不过兼容性怎么样就没有测试了:)
<html> <head> <title>Widget test</title> <style type="text/css"> table{ border-collapse: collapse; border: 1px solid black; } table td{ border: 1px solid black; } </style> </head> <body> <form> <table id="myTable"/> </form> <script > function b(){ // 构建table 10X10 var table = document.getElementById("myTable"); var i;var j; for (i=0;i<5;i++){ var row = table.insertRow(i); for (j=0;j<5;j++){ var cell = row.insertCell(j); cell.innerHTML = i+ "X" +j; cell.addEventListener("click", function do_click(){ addInput(this); }); } } } function closeInput(elm) { var td = elm.parentNode; var value = elm.value; td.removeChild(elm); td.innerHTML = value; } function addInput(elm) { if (elm.getElementsByTagName('input').length > 0) return; var value = elm.innerHTML; elm.innerHTML = ''; var input = document.createElement('input'); input.setAttribute('type', 'text'); input.setAttribute('value', value); input.setAttribute('onBlur', 'closeInput(this)'); input.setAttribute('onkeypress', 'return runScript(event)'); elm.appendChild(input); input.focus(); } function runScript(e) { if (e.keyCode == 13) { // closeInput(e.srcElement); e.srcElement.blur(); return false;// cancel event bubble } return true; } b(); </script> </body> </html>
简单说
function addText(button) { var textarea = document.getElementById('');// textarea.innerHTML = textarea.innerHTML + button.value; }
<input type='button' onclick='addText(this);' value='1' />
这样什么的。
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
没怎么写过js,刚好拿来玩玩。我的chrome 34 (mac 10.9) 上通过。能用,不过兼容性怎么样就没有测试了:)
简单说
这样什么的。