javascript - add color dynamically to added text - Stack Overflow

i am working on this example of appendChild() method.but the difference is here i am trying to add more

i am working on this example of appendChild() method.but the difference is here i am trying to add more text to a div dynamically.that was all right.but the hard part is the text i want to add will be red in color.how can i do that? i tried

  text.setAttributes('color',"red");

But it didn't work.so,how this task can be done??please help,thanks!!!!

the full code is given below............

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function create_text(){
    var mydiv = document.getElementById("mydiv");
    var text = document.createTextNode(" New text to add.");

    mydiv.appendChild(text);
}
</script>
</head>
<body>
<button onclick="create_text();">Create Text Node</button>
<div id="mydiv">Wele, here is some text.</div>
</body>
</html>

i am working on this example of appendChild() method.but the difference is here i am trying to add more text to a div dynamically.that was all right.but the hard part is the text i want to add will be red in color.how can i do that? i tried

  text.setAttributes('color',"red");

But it didn't work.so,how this task can be done??please help,thanks!!!!

the full code is given below............

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function create_text(){
    var mydiv = document.getElementById("mydiv");
    var text = document.createTextNode(" New text to add.");

    mydiv.appendChild(text);
}
</script>
</head>
<body>
<button onclick="create_text();">Create Text Node</button>
<div id="mydiv">Wele, here is some text.</div>
</body>
</html>
Share Improve this question asked Mar 26, 2014 at 20:17 AL-zamiAL-zami 9,07617 gold badges77 silver badges136 bronze badges 1
  • You can't apply styles directly to text nodes, and you don't set styles as attributes on elements. They need to go on the style object on the element. – cookie monster Commented Mar 26, 2014 at 20:20
Add a ment  | 

3 Answers 3

Reset to default 6

You would normally have to use CSS properties, however, text nodes cannot have CSS properties applied to them. You therefore need another container element:

You can choose any container element you wish, e.g. div, span, etc. It just needs to be capable of containing a text node. Having an element then allows us to access the styles property and set various styles (the color attribute in your case).

→ jsFiddle

function create_text(){
    var mydiv = document.getElementById("mydiv");

    var container = document.createElement("span");
    var text = document.createTextNode(" New text to add.");

    container.appendChild(text);
    container.style.color = "red";

    mydiv.appendChild(container);
}

Further note:

  • the order of the color assignments and calls of appendChild is arbitrary. The following would also be possible:

    function create_text(){
      var mydiv = document.getElementById("mydiv");
    
      var container = document.createElement("span");
      var text = document.createTextNode(" New text to add.");
    
      container.appendChild(text);
      mydiv.appendChild(container);
    
      container.style.color = "red";
    }
    
mydiv.style.color = 'red';

or just in css

#mydiv { color: red; }

if you have other elements inside the div that you don't want to be red, you'd need to wrap the new text in a span or div or another element before appending.

with jquery this would be super easy:

$('#mydiv').append('<span style="color:red">this is new text</span>');

If that's everything in your div, you could try

document.getElementById("mydiv").style.color="#ff0000";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742352994a4427885.html

相关推荐

  • javascript - add color dynamically to added text - Stack Overflow

    i am working on this example of appendChild() method.but the difference is here i am trying to add more

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信