原生的js实现99乘法表实现选择下拉框颜色,改变背景颜色为选中的颜色

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title>九九乘法表</title>
<style>
p {
width: 100px;
display: inline-block;
padding: 10px;
margin: 0;
font-size: 20px;
background-color: #FFFFFF;
border-top: 1px solid black;
border-right: 1px solid black;
}
div {
display: block;
}
#zheng99 {
width: auto;
display: inline-block;
border-left: 1px solid #000000;
border-bottom: 1px solid #000000;
}
#group{
display: block;
margin-top: 20px;
width: 200px;
height: 50px;
font-size: 20px;
}
#bianse{
display: inline-block;
width: 200px;
height: 60px;
margin-top: 20px;
font-size: 20px;
}
</style>
</head> <body>
<h1>9*9乘法表</h1>
<div id="zheng99"></div>
<select id="group">
<option value="red">红色</option>
<option value="yellow">黄色</option>
<option value="green">绿色</option>
</select>
<button onclick="changeColor()" id="bianse">改变颜色</button>
</body>
<script>
window.onload = function() {
for(let i = 1; i <= 9; i++) {
divs = document.getElementById("zheng99").innerHTML += "<div></div>";
for(let j = 1; j <= i; j++) {
document.getElementById("zheng99").getElementsByTagName("div")[i - 1].innerHTML +=
"<p>" + j + " x " + i + " = " + (j * i) + "</p>";
}
}
};
function changeColor() {
let tag = document.getElementsByTagName('p');
let select = document.getElementById('group');
let index = select.selectedIndex;
let group = select.options[index].value;
for (let i = 0;i < tag.length; i++){
tag[i].style.backgroundColor = group;
}
}
</script> </html>

在这段代码中用到的修改i颜色的核心代码是

<select id="selectColor">
<option value="red">红色</option>
<option value="yellow">黄色</option>
<option value="green">绿色</option>
</select>

  

function changeColor() {
let tag = document.getElementsByTagName('p');
let select = document.getElementById('group');
let index = select.selectedIndex;
let group = select.options[index].value;
for (let i = 0;i < tag.length; i++){
tag[i].style.backgroundColor = group;
}
}

  小伙伴们可以自己试试

最新文章

  1. Unity 好坑的Save Scene
  2. PHP中curl_init函数用法
  3. iOS开发之功能模块--推送之坑问题解决
  4. iOS开发 GET、POST请求方法(NSURLSession篇)
  5. 编写更好的C#代码
  6. rpm 更新/升级 软件包(libGL-devel手动安装过程)
  7. JavaScript DOM高级程序设计 2.4-try{}catch{}--我要坚持到底!
  8. 关于正则表达式的转义 PHP
  9. 开心菜鸟学习系列学习笔记------------nodejs util公共函数
  10. MySQl 存储过程+游标
  11. iOS自动打包并发布脚本
  12. nyoj_5:Binary String Matching
  13. 常用校验码(奇偶校验,海明校验,CRC)学习总结
  14. Android进阶(二十五)setTextColor()的参数设置方式
  15. mybatis整合spring获取配置文件信息出错
  16. Entity Framework 异常: &#39;OFFSET&#39; 附近有语法错误。\r\n在 FETCH 语句中选项 NEXT 的用法无效。
  17. dotnetcore ueditor
  18. InetAddress and InetSocketAddress
  19. Java中的引用传递和值传递
  20. python unicode

热门文章

  1. position:sticky 粘性定位的几种巧妙应用
  2. JDBC_11_PreparedStatement(增删改操作)
  3. Nest 中处理 XML 类型的请求与响应
  4. 2. robot framework 关键字,变量,循环
  5. 1.7.6- 浏览器审查HTML标签元素
  6. 指定pdf的格式
  7. hdu4544 优先队列(小贪心)
  8. 音视频开发:为什么推荐使用Jetpack CameraX?
  9. 【python】Leetcode每日一题-位1的个数
  10. Ubuntu20.04连接WiFi