我正在尝试将一些依赖于 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/