gpt4 book ai didi

Javascript Jquery div .text 在 javascript 中赋值

转载 作者:行者123 更新时间:2023-11-30 09:30:23 24 4
gpt4 key购买 nike

我正在尝试将一些依赖于 jQuery 的 can 更改为纯 JS,我想知道如何翻译这个:

$("#mydiv .car span").text(myVariable);

上面的代码在纯 JS 中会是什么?

最佳答案

您可以使用 document.querySelector() 来做到这一点textContent像这样:

document.querySelector("#mydiv .car span").textContent = myVariable;

或使用 innerText像这样:

document.querySelector("#mydiv .car span").innerText = myVariable;

MDN Reference for document.querySelector()

Returns the first Element within the document that matches the specified selector, or group of selectors.

这使得它类似于 jQuery,可以使用这种选择器。

关于Javascript Jquery div .text 在 javascript 中赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46636316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com