我需要一个代码,可以在单击第一个表元素时打开新页面。到目前为止,我设法完成了这项工作:<script>var url = 'https://www.google.com/';var element = document.getElementsByClassName('xl9624916')[1]; element.addEventListener('click',function(){ location.href = url;}); </script>但这对我来说不是最好的解决方案,因为此页面是从 Excel 发布的,并且元素名称在每次重新发布事件时都会发生变化。因此需要单击特定的表子项。请帮我理解下面的代码有什么问题?<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贡献1804条经验 获得超8个赞
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 关注
- 68 浏览
添加回答
举报
0/150
提交
取消