简体   繁体   English

如何使用clear javascript插入图片?

[英]how to insert a picture using clear javascript?

Schematically what I want.示意性地我想要什么。 How to do it?怎么做?

<img src=incamingMessage>
<script>
  var socket = new WebSocket("ws://localhost:9090/");

  socket.onmessage = function(event) {
    var incomingMessage = event.data;
    insertPic(incomingMessage);
  };

  function insertPic(message) {
    //here something that change <img>
  };

  //event.data = "img/312.png"
</script>

update added listener.更新添加的侦听器。 i dont know how to change arg in img我不知道如何更改 img 中的 arg

At first, who is event.data ?首先,谁是event.data What data type?什么数据类型? What kind of variable?什么样的变量? Where is it defined?它在哪里定义? Is it an array of bytes?它是一个字节数组吗? is it a var ?var吗?

What you do is probably better doable with PHP like this:你所做的可能更适合 PHP 这样的:

<?php

$incomingMessage = // populate with the actual data from your source.

echo '<img src="'.incomingMessage'">';

?>

As for Javascript, I have no idea what to tell you because you did not provide any info about the source of the data, the variable that stores it, etc.至于Javascript,我不知道该告诉你什么,因为你没有提供任何关于数据来源、存储数据的变量等信息。

document.getElementById(id).src = incomingMessage; document.getElementById(id).src = incomingMessage; that what i wanted to know那是我想知道的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM