1

I have a query related to localization of Chrome extensions. I am trying to localize the text in a button in a popup html file in my extension. The code I am using is as follows:

<button name="buttonSave" id="buttonSave" style="width: 100px; float: right;">
       <label class="buttonTextStyle">

       </label>
</button>

The javascript code is as follows:

document.getElementById("buttonSave").innerText = chrome.i18n.getMessage("SaveButton");

I have tried using innerText, innerHTML, value but the text in the button is not applied. Am I doing something wrong here?

4

1 回答 1

3

我只会使用<input type="button">并设置value按钮的属性:

<input type="button" name="buttonSave" id="buttonSave" style="width: 100px; float: right;">

....

document.getElementById("buttonSave").value = chrome.i18n.getMessage("SaveButton");

顺便说一句,我相当肯定在 a 中<label>嵌套的<button>不是有效的 HTML。使用<div>or<span>代替。

编辑

我刚刚做了一个测试,使用其中一个.innerHTML.innerText应该可以正常工作。你确定:

  1. 的返回值chrome.i18n.getMessage("SaveButton")是非空的?

  2. document.getElementById("buttonSave")在 DOM 构建后运行(即,document.getElementById("buttonSave")不为空或抛出错误)?

此外,请了解覆盖按钮的innerTextorinnerHTML会破坏按钮内已有的元素。也许你真的document.querySelector("#buttonSave .buttonTextStyle").innerText想写嵌套在按钮内的元素?

于 2012-07-23T13:30:11.780 回答