How do I add style to content in JavaScript?

后端 未结 4 1811
独厮守ぢ
独厮守ぢ 2021-01-13 08:02

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 swap

4条回答
  •  不要未来只要你来
    2021-01-13 08:41

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

提交回复
热议问题