Javascript Jquery div .text 在 javascript 中赋值

标签 javascript jquery html dom

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

相关文章:

javascript - JQuery 函数对单选按钮不响应

javascript - 无法将 JS 转换为 Typescript

javascript - 将 Google Maps VueJS 组件 (vue2-google-maps) 集成到 Laravel Blade View 中

javascript - Threejs 变换矩阵排序

javascript - 在鼠标悬停或调整浏览器大小之前不会加载内容

javascript - 带有更多链接的 Bootstrap Navigation.. 试图用 jquery 找到解决方案

javascript - AngularJS- ionic : create a swipeable horizonatlview which will update vertical view

javascript - Jquery .append 和appendto() 悲伤

html - 当文本达到我的 td 中图像的宽度时,如何在我的 td 换行中制作文本?

javascript - 下拉信息框