如何在 JavaScript 中向内容添加样式?
我一直在处理 JavaScript 文件,并且我的内容一直在处理短语。现在我想改变这些短语的风格。第一个函数(参见函数swapFE)我想将短语节点的字体样式更改为正常。并将短语节点的颜色更改为颜色值(155,102,102)。第二个函数(参见swapEF)我想将字体样式更改为斜体,将字体颜色更改为黑色。我该如何写这些?我是在 JavaScript 中将其编写在这些函数中,还是直接在 CSS 或 HTML 中应用样式更改?
这是我想要应用样式更改的两个函数:
//this function changes the French phrase to an English phrase.
function swapFE(e) {
var phrase = e.srcElement;
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
//childNodes[0] is the number of the phrase +1
var idnum = parent.childNodes[0];
//parseInt takes a textstring and extracts it to make a number. Then you will subtract 1 from the number.
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = english[phrasenum];
}
function swapEF(e) {
var phrase = e.srcElement;
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
var idnum = parent.childNodes[0];
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = french[phrasenum];
如果您甚至可以向我指出一个参考,我可以在其中找到这些属性,那就太好了。
I have been working in a JavaScript file and my content has been working with phrases. Now I want to change the style of those phrases. The first function (see function swapFE) I want to change the font style of the phrase node to normal. And change the color of the phrase node to the color value (155, 102, 102). The second function (see swapEF) I want to change the font style to italic and the font color to black. How do I write these? And do I write it within my those functions in JavaScript or do style changes get applied directly within CSS or HTML?
These are the two functions I want to apply the style changes to:
//this function changes the French phrase to an English phrase.
function swapFE(e) {
var phrase = e.srcElement;
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
//childNodes[0] is the number of the phrase +1
var idnum = parent.childNodes[0];
//parseInt takes a textstring and extracts it to make a number. Then you will subtract 1 from the number.
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = english[phrasenum];
}
function swapEF(e) {
var phrase = e.srcElement;
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
var idnum = parent.childNodes[0];
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = french[phrasenum];
If you could even just point me to a reference where I can find these properties that'd be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
例如:
您要查找的 google 关键字是 HTML DOM(文档对象模型),它将各种样式定义为对象样式成员的属性。
for instance:
The google keyword you're looking for is HTML DOM (document object model), which defines the various styles as properties of an object's style member.
类更改为元素 ..
您还可以在 swapFE 函数
、swapEF 函数
和 css 文件中将
You could also just change a class to the element ..
in the swapFE function
and in the swapEF function
and in your css file
我想出了如何添加样式更改。通过编写parent.childNodes[1].style.fontStyle=“正常”或“斜体”(取决于功能);并且parent.childNodes[1].style.color =“黑色”。
I figured out how to add the style changes. By writing parent.childNodes[1].style.fontStyle= "normal" or "italic" (depending on the function); and parent.childNodes[1].style.color = "black".
使用
element.style
您可以从 Javascript 更改 css。例如:
为了帮助您找到该属性的确切语法,这里有 CSS 属性到 JavaScript 引用的转换。
Using
element.style
you can change css from Javascript.For example:
And to help you find the exact syntax for the attribute, here's CSS Properties To JavaScript Reference Conversion.