【发布时间】:2020-09-08 13:35:22
【问题描述】:
问题
我很难在我的网站上更改按钮的文本。
<div> 上没有我想要定位的课程,这就是为什么我在为这个编辑而苦苦挣扎的原因。
说实话,我对 JS 有一些基本的了解,所以我可以理解,但是我自己还没有找到这个解决方案。
有页面的简化代码:
<div class="notion-topbar">
<div>
<div class="notranslate">...</div>
<div>...</div>
<div role="button"> // I want to change the text in this <div>!
<svg>...</svg> my_text
</div>
如果您愿意,可以在此处观看完整页面并搜索“topbar”:https://overwatch-guide.com 我想把右上角的“搜索”按钮翻译成法语。
可能的解决方案
我可能会找到解决方案的一部分,例如使用 document.querySelector('input[type=button]').innerHTML = "Rechercher";,但我知道缺少某些内容,因为我不想选择页面上的所有按钮。
我还在 CSS(我更熟悉的一种语言)中发现了一种“hack”,但我不是这种解决方案的忠实拥护者,因为 CSS 不是用来编辑文本的。就我而言,我想象过这样的:
.notion-topbar > div:nth-child(1n+3) > .button {
visibility: hidden
}
.notion-topbar > div:nth-child(1n+3) > .button:after {
visibility: visible;
display: block;
content: 'Rechercher';
position: absolute;
}
帮助
我真的可以使用你的帮助,因为我在这方面很难挣扎。 感谢您的宝贵时间!
【问题讨论】:
-
任何 CSS 选择器都可以在 JS 中使用。因此,您的“CSS hack”中的选择器可以在
document.querySelector中使用,或者只需右键单击 devtools 中的元素,然后单击 -> 复制 -> 复制选择器 -
我不太确定在我的脚本中使用 CSS 选择器。那么,你的意思是我可以尝试这样的脚本吗? document.querySelector('div.notion-topbar > div > div:nth-child(3)').innerHTML = "Rechercher";
-
如果这是正确的 CSS 选择器,那么可以。小提示,使用
textContent而不是innerHTML,因为它有一些重大的安全问题 -
感谢
textContent的建议!我试过这个脚本,但我不知道为什么它不起作用... document.querySelector(".notion-topbar > div:nth-child(1) > div:nth-child(3)").textContent = "Rechercher"; -
它适用于我,只需将其复制粘贴到控制台中。
标签: javascript html class dom button