如何在 JavaScript 中向内容添加样式?

发布于 2024-08-19 09:22:05 字数 1091 浏览 4 评论 0原文

我一直在处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撕心裂肺的伤痛 2024-08-26 09:22:05
obj.style.whicheverProperty = "value"

例如:

document.getElementById('mydiv').style.fontVariant = "italic";

您要查找的 google 关键字是 HTML DOM(文档对象模型),它将各种样式定义为对象样式成员的属性。

obj.style.whicheverProperty = "value"

for instance:

document.getElementById('mydiv').style.fontVariant = "italic";

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.

树深时见影 2024-08-26 09:22:05

类更改为元素 ..

您还可以在 swapFE 函数

phrase.className = 'english';

、swapEF 函数

phrase.className = 'french';

和 css 文件中将

.english{ color:#9b6666; }
.french{ font-style:italic; color:#000; }

You could also just change a class to the element ..

in the swapFE function

phrase.className = 'english';

and in the swapEF function

phrase.className = 'french';

and in your css file

.english{ color:#9b6666; }
.french{ font-style:italic; color:#000; }
笑忘罢 2024-08-26 09:22:05

我想出了如何添加样式更改。通过编写parent.childNodes[1].style.fontStyle=“正常”或“斜体”(取决于功能);并且parent.childNodes[1].style.color =“黑色”。

//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];
       parent.childNodes[1].style.fontStyle= "normal";
         //Below is the correct way to write an RGB style.
         parent.childNodes[1].style.color = "rgb(155, 102, 102)";
}


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];
       parent.childNodes[1].style.fontStyle= "italic";
       parent.childNodes[1].style.color= "black";

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".

//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];
       parent.childNodes[1].style.fontStyle= "normal";
         //Below is the correct way to write an RGB style.
         parent.childNodes[1].style.color = "rgb(155, 102, 102)";
}


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];
       parent.childNodes[1].style.fontStyle= "italic";
       parent.childNodes[1].style.color= "black";
撩动你心 2024-08-26 09:22:05

使用 element.style 您可以从 Javascript 更改 css。

例如:

document.getElementById('mydiv').style.backgroundColor = 'red';

为了帮助您找到该属性的确切语法,这里有 CSS 属性到 JavaScript 引用的转换

Using element.style you can change css from Javascript.

For example:

document.getElementById('mydiv').style.backgroundColor = 'red';

And to help you find the exact syntax for the attribute, here's CSS Properties To JavaScript Reference Conversion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文