pppguest3962
狐狸大王
狐狸大王
  • UID26872
  • 注册日期2008-11-01
  • 最后登录2023-08-16
  • 发帖数343
  • 经验158枚
  • 威望0点
  • 贡献值236点
  • 好评度5点
阅读:957回复:2

请教油猴的JS(修改一个表格td的宽度)

楼主#
更多 发布于:2020-12-08 11:17
页面上有个表格的单元格宽度,想修改,请教代码关键字
第一个问题是:油猴能否通过xpath定位去修改参数?

<td rowspan="3" width="20%">具体</td>
这个代码在整个html,是唯一的
想把width修改成45%
xpath的定位是
/html/body/div[6]/div[4]/div[1]/div/table/tbody/tr[1]/td[11]

如果不能xpath定位它,那么应该历遍<a>,找到关键字“具体”,再去修改吗?
请教思路,谢谢大家!!
逗妇乳
小狐狸
小狐狸
  • UID50148
  • 注册日期2015-06-01
  • 最后登录2024-04-20
  • 发帖数86
  • 经验92枚
  • 威望0点
  • 贡献值32点
  • 好评度7点
1楼#
发布于:2020-12-08 22:49
引入Jquery
$('table td').each(function(){
    if ($(this).contains('具体'))
        $(this).css("width", "45%");
})
taoww
非常火狐
非常火狐
  • UID39284
  • 注册日期2013-03-18
  • 最后登录2024-04-24
  • 发帖数627
  • 经验573枚
  • 威望0点
  • 贡献值110点
  • 好评度99点
2楼#
发布于:2020-12-09 12:28
现在不是十多年前了,xpath在定位方面的功能css selector基本都可以取代了,用起来比xpath简便得多
document.querySelector("html&gt;body&gt;div:nth-child(7)&gt;div:nth-child(5)&gt;div:nth-child(2)&gt;div&gt;table&gt;tbody&gt;tr:nth-child(2)&gt;td:nth-child(12)").setAttribute("width", "45%")
注意:nth-child()是从1开始计数的
也可以简化一下路径,没必要每一层都写出来
document.querySelector("body&gt;div:nth-child(7)&gt;div:nth-child(5)&gt;div:nth-child(2) tbody&gt;tr:nth-child(2)&gt;td:nth-child(12)").setAttribute("width", "45%")
还可以通过属性而不是路径来定位
document.querySelector('td[rowspan="3"][width="20%"]').style.width = "45%"
游客

返回顶部