调整元素高度的JavaScript代码在控制台有效,书签脚本却失效
书签脚本失效:无法读取undefined的style属性
我有一段可动态调整盒子高度的代码,在浏览器控制台执行完全正常:
document.getElementsByClassName("tox tox-tinymce")[0].style.height = '700px';
但将这段代码制作成书签脚本后,点击书签就会报错:Uncaught TypeError: Cannot read properties of undefined (reading 'style')
我试过两种书签脚本写法:
javascript:(function(){document.getElementsByClassName("tox tox-tinymce")[0].style.height = '700px'})() // 也尝试过在线书签脚本转换工具生成的编码版本 // javascript:(function()%7Bdocument.getElementsByClassName(%22tox%20tox-tinymce%22)%5B0%5D.style.height%20%3D%20'700px'%7D)()
这个脚本一周前还能正常工作,如今突然失效,我找不到原因。
我尝试逐层遍历DOM排查:
all = document.querySelectorAll('body')[0]; console.log(all); macro=all.querySelectorAll('*')[0]; console.log(macro); editbox = macro.querySelectorAll('*')[0]; console.log(editbox)
遍历结果:
all的输出是"</body"macro是页面首个元素——一个名为<macropotent-somethingsomething...>的外部包裹标签,但用querySelectorAll查询该标签内部元素时,返回空的NodeList[]
内容的提问来源于stack exchange,提问作者not_a_generic_user
相关产品推荐
相关产品推荐

