获取元素通过 data-i18n-key

huangapple go评论63阅读模式
英文:

Get element by data-i18n-key

问题

以下是已经翻译好的内容:

我试图获取由外部应用程序添加的元素我能够获取这个特定的元素的唯一方法是通过data-i18n-key属性我以为我可以像获取任何数据属性一样获取它就像这样

这段代码

```javascript
const buttons = document.querySelectorAll('[data-i18n-key="sdk.seamless_product_reward.paid_items_required"]');
console.log(buttons.length);

然而,这并没有返回任何内容。有没有办法可以做到这一点?


<details>
<summary>英文:</summary>

I&#39;m trying to get an element which is added by an external application. The only way I can get this specific element is by the data-i18n-key attribute which I thought I can grab like any data attribute so something like this.

The code:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    const buttons = document.querySelectorAll(&#39;[data-i18n-key=&quot;sdk.seamless_product_reward.paid_items_required&quot;]&#39;);
    console.log(buttons.length);

&lt;!-- language: lang-html --&gt;

    &lt;span class=&quot;lion-reward-item__redeem-button-text lion-loyalty-page-reward-item__redeem-button-text&quot; data-i18n-key=&quot;sdk.seamless_product_reward.paid_items_required&quot;&gt;Paid items required&lt;/span&gt;

&lt;!-- end snippet --&gt;

However, this doesn&#39;t return anything. Any ideas how to do this?

</details>


# 答案1
**得分**: 1

当然,Barmer是绝对正确的。你的代码有效。问题在于你的JS在DOM完成加载之前就被初始化了。将你的JS放在闭合的`</body>`标签之上。

<details>
<summary>英文:</summary>

Of course, Barmer is absolutely right. Your code works. The problem will be that your JS is initialised before the DOM has finished loading. Pack your JS above the closing body tag.

</details>



huangapple
  • 本文由 发表于 2023年2月14日 00:13:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定