最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - inserting a variable on style tag - Stack Overflow

programmeradmin1浏览0评论

I need to change the color of the text based on the drop down list selection.

<select id="room2">
    <option>#0808cf</option>
    <option>#0E9E26</option>
</select>
<input type="text" id="txtColor">
   John: <p style="color:#0808cf" > test </p>

jquery

$('#colors').change(function(){
   $('#txtColor').val($("#colors").val());
   var fontColor =  $('#txtColor').val();

});

I dont want the change to be in the css cause the select id will not be constant. I want it to be inserted in the p style tag. And also i need the text to be John: test to be in one line. I tried this but not working. Thank you.

<p style="color:"+fontColor+" > test </p>

demo: /

I need to change the color of the text based on the drop down list selection.

<select id="room2">
    <option>#0808cf</option>
    <option>#0E9E26</option>
</select>
<input type="text" id="txtColor">
   John: <p style="color:#0808cf" > test </p>

jquery

$('#colors').change(function(){
   $('#txtColor').val($("#colors").val());
   var fontColor =  $('#txtColor').val();

});

I dont want the change to be in the css cause the select id will not be constant. I want it to be inserted in the p style tag. And also i need the text to be John: test to be in one line. I tried this but not working. Thank you.

<p style="color:"+fontColor+" > test </p>

demo: http://jsfiddle/kX3EN/

Share Improve this question edited Apr 5, 2013 at 11:13 Rob 4,94712 gold badges52 silver badges56 bronze badges asked Jul 18, 2012 at 19:50 deidara songdeidara song 2093 gold badges5 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Try - http://jsfiddle/kX3EN/7/

$('#colors').change(function(){
    $('p').css( 'color', $(this).val() );
});

If you want your "John: test" to be on the same line, you need to:

  • Change the p (block-level) to something like a span (inline) or
  • Force the p to act as inline with css (display: inline).

Using jQuery, you need to use the css() function to change the style attribute of an element. Like so:

$('selector for element you want to change').css('color', $("#your-select-element").val());

You'll probably put this in an event handler for your select:

$("select#colors").change(function() {
    $("span.changemycolor").css('color', $(this).val());
    // 'this', in this case, is your select element
});

got it working by placing the tag after my html code filter. So everytime I append a message will be stored as variable and read as JS when the var is sent to the websocket send method. Thank you

发布评论

评论列表(0)

  1. 暂无评论