我需要一個(gè)代碼,可以在單擊第一個(gè)表元素時(shí)打開新頁面。到目前為止,我設(shè)法完成了這項(xiàng)工作:<script>var url = 'https://www.google.com/';var element = document.getElementsByClassName('xl9624916')[1]; element.addEventListener('click',function(){ location.href = url;}); </script>但這對我來說不是最好的解決方案,因?yàn)榇隧撁媸菑?Excel 發(fā)布的,并且元素名稱在每次重新發(fā)布事件時(shí)都會發(fā)生變化。因此需要單擊特定的表子項(xiàng)。請幫我理解下面的代碼有什么問題?<script>var url = 'https://www.google.com/';var element = document.querySelectorAll("table tr:nth-child(1) td:nth-child(1)");element.addEventListener('click',function(){location.href = url;}); </script>
1 回答

胡說叔叔
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
Insted of using the querySelectorAll use querySelector
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
var url = 'https://www.google.com/';
var element = document.querySelector("table tr:nth-child(1) td:nth-child(1)");
element.addEventListener('click',function(){
location.href = url;
});
- 1 回答
- 0 關(guān)注
- 117 瀏覽
添加回答
舉報(bào)
0/150
提交
取消