Javascript 我可以给jQuery变量一个类,然后更改该类的字体颜色吗

Javascript 我可以给jQuery变量一个类,然后更改该类的字体颜色吗,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,所以,当我附加某个变量时,它总是一个单词,但和其他单词在一个字符串中,它将是不同的颜色 但是在过去的5个小时里一直在寻找这样做的方法…:( 咖啡锉 App.room = App.cable.subscriptions.create "RoomChannel", connected: -> # Called when the subscription is ready for use on the server disconnected: -

所以,当我附加某个变量时,它总是一个单词,但和其他单词在一个字符串中,它将是不同的颜色

但是在过去的5个小时里一直在寻找这样做的方法…:(

咖啡锉

    App.room = App.cable.subscriptions.create "RoomChannel",
      connected: ->
        # Called when the subscription is ready for use on the server

      disconnected: ->
        # Called when the subscription has been terminated by the server

      received: (data) ->
        $('#messages').append data['message']


      speak: (message) ->
        @perform 'speak', message: message



    $(document).on 'keypress', '[data-behavior~=room_speaker]', (event) ->
      if event.keyCode is 13 # return/enter = send
        App.room.speak(name + ":"  + event.target.value )
        event.target.value = ''
        event.preventDefault()
Js文件

    $(document).ready(function(){
      $("#user_button").on('click', function(e){
        e.preventDefault();
        name = $("#username").val()
        $('#chat-input').show();
        $('#chat-username').hide();
      });

我希望名称变量是不同的颜色。

案例1:如果您的用户名包含在other['name']中,请执行下面的操作

received: (data) ->
  $('#messages').append data['message'],'<span style=\'color:red\'>' + other['name'] + '</span>'
接收:(数据)->
$('#messages')。附加数据['message'],''+其他['name']+''
案例2:如果其包含在消息中,则以样式存储

  name_with_color = '<span style=\'color:red\'>' + name + '</span>'
  App.room.speak(name_with_color + ":"  + event.target.value )
name_,颜色=“”+name+“”
App.room.speak(使用颜色+“:”+event.target.value命名)

请给我们看看你的代码,你能举个例子吗?我真的不明白你想做什么。答案是肯定的,你可以
“我希望name变量是不同的颜色。”
-什么?name变量将完全包含你设置的内容,
$(“#username”).val()
。(好吧,一旦你纠正了你的输入错误并在语句末尾加了一个分号。)但是你什么都不用
name
。我不清楚你试图描述.App.room.speak(name+”:“+event.target.value)的问题是什么,使用case2:不幸的是,这会在留言板上打印“嘿:”。