135.DOM效果实现:表格的隔行变色

效果图:

 第一个表格点击后一列的表格会变色

第二个表格隔行变色

第三个表格点击后一行的表格会变色

三个表格的基本样式:

css部分

<style>
			table{
				border: 1px red solid;
			}
			tr{
				width: 100%;
				height: 40px;
			}
			td{
				width: 100px;
				border: 1px darkred solid;
			}
		</style>
		

 html部分

		<table id="tb" cellspacing="0">
			<tr>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
			</tr>
			<tr>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
			</tr>
			<tr>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
			</tr>
			<tr>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
				<td>hello</td>
			</tr>
		</table>

 1.点击横排变色

1.把所有的td的背景颜色设置为白色

2.获取当前点击的元素是一横排中的第x个元素

3.把每一横排的第x个元素 设置为red

先获取到每一个需要用到的节点

var tb=document.querySelector("#tb")
var trs=document.querySelectorAll("tr")
var tds=document.querySelectorAll("td")

设计点击事件

tds[i].onclick=function(){}

先把所有的td的背景颜色设置为白色

tds.forEach((el)=>{
						el.style.backgroundColor="white"
					})

设计一段函数以获取x

Object.prototype.indexof1=function(){
				var arr=this.parentElement.children
				for(let i=0;i<arr.length;i++){
					if(arr[i]===this){
						return i
					}
				}				 
			}

使用let x=tds[i].indexof1()可以获得x

第x个元素 设置为red

trs.forEach((el)=>{
						//el是tr元素
						el.children[x].style.backgroundColor="red"
					})

又因为是每个横排都要执行一遍

加个for循环

for(let i=0;i<tds.length;i++){
				tds[i].onclick=function(){
					// console.log(this,tds[i])
					//1.把所有的td的背景颜色设置为白色
					//2.获取当前点击的元素是一横排中的第x个元素
					//3.把每一横排的第x个元素 设置为red
					//排它思想
					tds.forEach((el)=>{
						// console.log(el,11)
						el.style.backgroundColor="white"
					})
					let x=tds[i].indexof1()
					trs.forEach((el)=>{
						//el是tr元素
						el.children[x].style.backgroundColor="red"
					})
				}
			}

2.静态隔行变色

1.获取tr元素的集合trs

var trs=document.querySelectorAll("#tb2 tr")

2.用for循环将元素遍历处理来用if判断奇偶数

if(i%2){

					}

3.修改元素的值

						trs[i].style.backgroundColor="gray"

3.点击纵列变色

1.把所有的tr的背景颜色设置为白色

el.style.backgroundColor="white"

2.检测点击事件tr的集合第i个元素被点击然后设置为灰色

trs[i].onclick=function(){
							trs[i].style.backgroundColor="gray"
						}

3.每一行都重复一次

(()=>{var trs=document.querySelectorAll("#tb3 tr")
				for(let i=0;i<trs.length;i++){					
						trs[i].onclick=function(){
							trs.forEach((el)=>{
								el.style.backgroundColor="white"
							})							
							// console.log(i,trs[i],11111)
							trs[i].style.backgroundColor="gray"
						}
					
				}
			})()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
{ type: "searchSelect", placeholder: "签约机构", valueName: 'signOrganId', optionName: "label", searchItemName: "label", optionId: "key", searchApi:commonService.orgPageList({}).then(res=>{ const {retData}=res retData.map(item=>{ return {key: item.id, label: item.organName, value: item.id,} }) }) }, 分析一下此段代码的报错 汉语解释index.jsx:55 Uncaught TypeError: item.searchApi is not a function at searchQuery (index.jsx:55:1) at onFocus (index.jsx:129:1) at onContainerFocus (BaseSelect.js:326:1) at HTMLUnknownElement.callCallback (react-dom.development.js:188:1) at Object.invokeGuardedCallbackDev (react-dom.development.js:237:1) at invokeGuardedCallback (react-dom.development.js:292:1) at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:306:1) at executeDispatch (react-dom.development.js:389:1) at executeDispatchesInOrder (react-dom.development.js:414:1) at executeDispatchesAndRelease (react-dom.development.js:3278:1) at executeDispatchesAndReleaseTopLevel (react-dom.development.js:3287:1) at forEachAccumulated (react-dom.development.js:3259:1) at runEventsInBatch (react-dom.development.js:3304:1) at runExtractedPluginEventsInBatch (react-dom.development.js:3514:1) at handleTopLevel (react-dom.development.js:3558:1) at batchedEventUpdates$1 (react-dom.development.js:21871:1) at batchedEventUpdates (react-dom.development.js:795:1) at dispatchEventForLegacyPluginEventSystem (react-dom.development.js:3568:1) at attemptToDispatchEvent (react-dom.development.js:4267:1) at dispatchEvent (react-dom.development.js:4189:1) at unstable_runWithPriority (scheduler.development.js:653:1) at runWithPriority$1 (react-dom.development.js:11039:1) at discreteUpdates$1 (react-dom.development.js:21887:1) at discreteUpdates (react-dom.development.js:806:1) at dispatchDiscreteEvent (react-dom.development.js:4168:1)
最新发布
07-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值