我正在用javascript創(chuàng)建一個(gè)小的測(cè)驗(yàn)類(lèi)型的應(yīng)用程序。我的html結(jié)構(gòu)看起來(lái)像這樣。<div class="btn-group-toggle" data-toggle="buttons"> <label class="btn btn-secondary btn-insight" data-target="#myCarousel" data-responseID="1"> <input type="radio" name="options" id="option1" autocomplete="off"> <h5>1</h5> <p class="mb-0">label for 1</p> <div class="line-bottom gradient-purple"></div></label>...</div>我正在嘗試使用自定義數(shù)據(jù)屬性data-responseID來(lái)確定用戶(hù)提供的答案。程序啟動(dòng)時(shí),使用querySelectorAll單擊偵聽(tīng)器并在每個(gè)偵聽(tīng)器上附加標(biāo)簽來(lái)遍歷標(biāo)簽。const responseLables = document.querySelectorAll('div.btn-group-toggle > label');responseLables.forEach(element => { element.addEventListener('click', function(e){ const clickedResponse = element.attributes[2].value; determineWhereToSlide(clickedResponse); });});這在Firefox和Chrome中效果很好,但在Edge中則不行。(我不關(guān)心IE 11)defineWhereToSlide只是一個(gè)提供警報(bào)的函數(shù)。最終,它將被用來(lái)推動(dòng)旋轉(zhuǎn)木馬前進(jìn)。我已經(jīng)制作了一個(gè)有效的示例,如果您在其他瀏覽器中打開(kāi)它,則可以看到該問(wèn)題。https://codepen.io/anon/pen/dLmQKZ?editors=1010我不知道為什么會(huì)這樣。
為什么element.attribute在Edge中不起作用?
胡說(shuō)叔叔
2021-04-30 10:07:58