亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將變量分配給單個表格單元格

將變量分配給單個表格單元格

九州編程 2021-06-29 09:51:52
我正在嘗試將圖像放入表格中。我有這個工作,如果你點擊,你會在可用的選項中循環。但是,計數器與單元格無關,它是一個全局計數器。有沒有辦法創建一個局部變量來跟蹤單個單元格在數組中的位置?我曾嘗試為一個單元格創建一個局部變量,但它不起作用。下面是js中的相關函數:var i = 1;var table = document.getElementsByTagName('table')[0];var def = ["tex/white.png", "tex/grass.jpg", "tex/stone.jpg", "tex/water.jpg", "tex/wood.jpg", "tex/lava.jpg"];table.onclick = function(e) {    var target = (e || window.event).target;    if (target.tagName in { TD: 1, TH: 1 }) {        target.className = "img";        console.log((e || window.event).target);        target.setAttribute('style', 'background-image:' + "url(" + def[i] + ")");        if (i < def.length) {            i++        }        if (i == def.length) {            i = 0;        }    }};這是我到目前為止所做的工作的小提琴:https ://jsfiddle.net/6L0armd4/期望的結果是我從單個單元格開始陣列,它只對這些單元格計數。目前,它總是提供下一個紋理,即使我選擇了不同的單元格。
查看完整描述

2 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

您需要制作偵聽器事件,您可以在下面的代碼中將顏色代碼更改為您的圖像路徑,并避免任何本地路徑圖像鏈接問題嘗試使用在線圖像直接鏈接。


var table = document.querySelector('#table')

var selectedCells = table.getElementsByClassName('selected')


table.addEventListener('click', function(e) {

var td = e.target


if (td.tagName !== 'TD') {

return

}


if (selectedCells.length) {

selectedCells[0].className = ''    

}


td.className = 'selected'

})

table {

 cursor: text;

}

tr {

background-color:white;

}

td {

font-size: 14;

cursor: default;

}


td.selected {

background-color: red;


 // replace this with background image tag like this  

 background-image: url("paper.gif



<table border="1" cellpadding="8" cellspacing="2" id="table">

<tr>

<td>Cell one</td>

<td>Cell Tw</td>

<td>Cell three</td>

<td>Cell Four</td>

<td>Cell five</td>

<td>Cell sex</td>

<td>Cell five</td>

<td>cell seven</td>

<td>cell eight</td>

<td>cell nine</td>

<td>cell tens</td>

<td>cell eleven</td>

<td>cell twelve</td>

</tr>

<tr>

<td>AKo</td>

<td>KK</td>

<td>KQs</td>

<td>KJs</td>

<td>KTs</td>

<td>K9s</td>

<td>K8S</td>

<td>K7s</td>

<td>K6s</td>

<td>K5s</td>

<td>K4s</td>

<td>K3s</td>

<td>K2s</td>


查看完整回答
反對 回復 2021-07-08
  • 2 回答
  • 0 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號