gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:20:41 25 4
gpt4 key购买 nike

因此,当我附加某个变量时,该变量始终是一个单词,但在带有其他单词的字符串中,它将是一种不同的颜色。

但在过去 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>'

情况2:如果它包含在消息中,则使用样式存储

  name_with_color = '<span style=\'color:red\'>' + name + '</span>'
App.room.speak(name_with_color + ":" + event.target.value )

关于javascript - 我可以给 jQuery 变量一个类,然后更改该类的字体颜色吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46585711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com