admin管理员组

文章数量:1399917

i know how to set font style italic in HTML but now i'm trying to do that only with java script . Is it possible to do that in java script ? . Can someone help\clarify me pls . Here is my code ,

 myJson.name.push(mainSteps.name); // Need to changes this in italic (js code)

i know how to set font style italic in HTML but now i'm trying to do that only with java script . Is it possible to do that in java script ? . Can someone help\clarify me pls . Here is my code ,

 myJson.name.push(mainSteps.name); // Need to changes this in italic (js code)
Share Improve this question edited Aug 16, 2016 at 12:22 Saravana Kumar asked Aug 9, 2016 at 5:56 Saravana KumarSaravana Kumar 1792 gold badges5 silver badges18 bronze badges 1
  • your mented out line should work... – Polyov Commented Aug 9, 2016 at 5:57
Add a ment  | 

7 Answers 7

Reset to default 4

A JavaScript string itself doesn't have a concept of a "style". Styling only applies when you output the string somewhere. If you output it to HTML, you can use HTML or CSS to style it.

So if you are asking whether there is an "output-agnostic" way to style a JavaScript string, the answer is no.


Btw, the code you wrote is JavaScript (assuming you pass a proper value for the ID):

document.getElementById(#Html id).style.fontStyle = "italic";

and if you want to style the HTML output, then this would be the way to go.

In Javascript you can not change font style because it does not exist in javascript as such. Font style matters only when the data has to be shown which is when it is written on some where in HTML .This is how you may change the fontstyle for entire body

    This is in italics
    <script>
    function italicsBody() {
        document.body.style.fontStyle = "italic";
    }
    italicsBody()
    </script>

Similary if you want the same for specifi data you may do it using getElemenById . Check the following code

<ul id="names">
    </ul>
    
    <script>
    function italicsBody() {
        document.getElementById("names").style.fontStyle = "italic";
    }
    function populateNames() {
      var names = ["name1","name","name3"]
      var nameList = document.getElementById("names");
      for (var key in names) {
          nameList.innerHTML = nameList.innerHTML + ' <li> ' + names[key] + '</li>';
      }
    }
    populateNames()
    italicsBody()
    
    </script>

Here is the jsfiddle for the same jsfiddle

document.getElementById("myP").style.fontStyle = "italic";

You can use "somestring".italics() to get "<i>somestring</i>"which might be what you are after. I suggest you use CSS as others said though - italics is not standard anymore.

I Given One Example As Below

function myFunction() {
    document.getElementById("myP").style.font = "italic 15px arial,serif";
}
myFunction();
<p id="myP">This is a paragraph.</p>

This Is Second Example When User Click On Button Then Style Applying From Java script .

function myFunction() {
    document.getElementById("myP").style.font = "italic 20px arial,serif";
}
<p id="myP">This is a paragraph.</p>

<button type="button" onclick="myFunction()">Set font</button>

myJson.name.push(mainSteps.name).style.fontStyle = "italic";

It is done the same way.

本文标签: How to set font style quotitalicquot using javascriptStack Overflow