在CSS(Node.js聊天)中为特定用户更改文本颜色

huangapple go评论67阅读模式
英文:

Change text colour for specific user in css (node js chat)

问题

我有一个简单的聊天室,用户可以互相聊天。我想要的是,如果用户1写了一些文本并发送,它将以白色显示给他自己,而其他用户发送的任何文本都将是黑色。我知道这可能有一个简单的CSS解决方案,但我无法弄清楚。以下是HTML代码。

<ul id="messages"></ul>
<form id="form" action="">
  <input id="input" autocomplete="off" /><button class="btn-primary"><b>Send</b></button>
</form>
<script src="/torrent-tracker/template/node_modules/socket.io/client-dist/socket.io.js"></script>
<script>
  var url = "http://someurl.net";
  const socket = io(url);

  var messages = document.getElementById('messages');
  var form = document.getElementById('form');
  let input = document.getElementById('input');
  var upime = "<?php echo $upime.": ";?>";
  var uporabnik = "<?php echo $upime;?>";

  socket.emit('newUser', uporabnik);

  form.addEventListener('submit', function(e){
    e.preventDefault();
    if(input.value){
      <?php last_action(); ?>
      socket.emit('PosljiSporocilo', upime+input.value);
      console.log(upime+input.value);
      input.value = '';
    }
  });

  socket.on('DobiSporocilo', Sporocilo => {
    var item = document.createElement('li');
    item.textContent = Sporocilo;
    messages.appendChild(item);
    window.scrollTo(0, document.body.scrollHeight);
  });

  socket.on('newUserJoined', joinedUser => {
    var item = document.createElement('li');
    item.textContent = joinedUser;
    messages.appendChild(item);
    window.scrollTo(0, document.body.scrollHeight);
  });
</script>

如果您有任何其他问题,请随时提出。

英文:

I have a simple chat room where users can chat to each other. What I want is that if user1 writes some text and sends it it will appear lets say in white for him and him only any other text from other users would be black colour. I know there is probably a simple CSS solution but I can't figure it out. Below is the HTML code.

&lt;ul id=&quot;messages&quot;&gt;&lt;/ul&gt;
&lt;form id=&quot;form&quot; action=&quot;&quot;&gt;
  &lt;input id=&quot;input&quot; autocomplete=&quot;off&quot; /&gt;&lt;button class=&quot;btn-primary&quot;&gt;&lt;b&gt;Send&lt;/b&gt;&lt;/button&gt;
&lt;/form&gt;
&lt;script src=&quot;/torrent-tracker/template/node_modules/socket.io/client-dist/socket.io.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
  var url = &quot;http://someurl.net&quot;;
  const socket = io(url);

  var messages = document.getElementById(&#39;messages&#39;);
  var form = document.getElementById(&#39;form&#39;);
  let input = document.getElementById(&#39;input&#39;);
  var upime = &quot;&lt;?php echo $upime.&quot;: &quot;;?&gt;&quot;;
  var uporabnik = &quot;&lt;?php echo $upime;?&gt;&quot;;

  socket.emit(&#39;newUser&#39;, uporabnik);

  form.addEventListener(&#39;submit&#39;, function(e){
    e.preventDefault();
    if(input.value){
      &lt;?php last_action(); ?&gt;
      socket.emit(&#39;PosljiSporocilo&#39;, upime+input.value);
      console.log(upime+input.value);
      input.value = &#39;&#39;;
    }
  });

  socket.on(&#39;DobiSporocilo&#39;, Sporocilo =&gt; {
    var item = document.createElement(&#39;li&#39;);
    item.textContent = Sporocilo;
    messages.appendChild(item);
    window.scrollTo(0, document.body.scrollHeight);
  });

  socket.on(&#39;newUserJoined&#39;, joinedUser =&gt; {
    var item = document.createElement(&#39;li&#39;);
    item.textContent = joinedUser;
    messages.appendChild(item);
    window.scrollTo(0, document.body.scrollHeight);
  });
&lt;/script&gt;

答案1

得分: 1

我猜$upime变量保存了一个用户名,消息看起来像&lt;li&gt;userName:blah-blah-blah&lt;/li&gt;。如果是这样,你需要修改消息监听器如下:

socket.on('DobiSporocilo', Sporocilo => {
  var item = document.createElement('li');
  item.textContent = Sporocilo;
  if (Sporocilo.match(new RegExp("^" + upime))){ item.classList.add('my-message');}
  messages.appendChild(item);
  window.scrollTo(0, document.body.scrollHeight);
});

并添加样式:

<style>
   .my-message{
      color:white;
   }
</style>
英文:

I guess the $upime variable holds a username and messages looks like &lt;li&gt;userName:blah-blah-blah&lt;/li&gt; If so, you need to modify on message listener this way

      socket.on(&#39;DobiSporocilo&#39;, Sporocilo =&gt; {
        var item = document.createElement(&#39;li&#39;);
        item.textContent = Sporocilo;
        if (Sporocilo.match(new RegExp(&quot;^&quot;+upime)){ item.classList.add(&#39;my-message&#39;);}
        messages.appendChild(item);
        window.scrollTo(0, document.body.scrollHeight);
      });

and add style

&lt;style&gt;
   .my-message{
      color:white;
   }
&lt;/style&gt;   

huangapple
  • 本文由 发表于 2023年4月7日 03:29:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定