Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在html中以div显示文本_Javascript_Html_Css - Fatal编程技术网

Javascript 在html中以div显示文本

Javascript 在html中以div显示文本,javascript,html,css,Javascript,Html,Css,我正在构建一个聊天应用程序,我想在聊天盒上显示一些文本 <div id="chatbox"></div> <input name="usermsg" type="text" id="usermsg" size="63" /> <button name="submitmsg" id="submitmsg">Send</button> 现在,当我在套接字上收到一条消息时,我想把它写到chatboxdiv socket.on('reply'

我正在构建一个聊天应用程序,我想在
聊天盒上显示一些文本

<div id="chatbox"></div>
<input name="usermsg" type="text" id="usermsg" size="63" />
<button name="submitmsg" id="submitmsg">Send</button>
现在,当我在套接字上收到一条消息时,我想把它写到
chatbox
div

socket.on('reply', function(msg) {
    console.log(msg)
    $('#chatbox').append('<br>' + $('<div/>').text(msg).html());
    });
index.css

#chatbox {
  text-align: left;
  margin: 0 auto;
  margin-bottom: 25px;
  padding: 10px;
  background: #fff;
  height: 270px;
  width: 430px;
  border: 1px solid #ACD8F0;
  overflow: auto;
}

.talk-bubble {
    margin: 40px;
  display: inline-block;
  position: relative;
    width: 150px;
    height: auto;
    background-color: lightyellow;
}
.border{
  border: 8px solid #666;
}
.round{
  border-radius: 30px;
    -webkit-border-radius: 30px;
    -moz-border-radius: 30px;

}

.tri-right.btm-right-in:after{
    content: ' ';
    position: absolute;
    width: 0;
    height: 0;
  left: auto;
    right: 38px;
    bottom: -20px;
    border: 12px solid;
    border-color: lightyellow lightyellow transparent transparent;
}

.tri-right.btm-right:after{
    content: ' ';
    position: absolute;
    width: 0;
    height: 0;
  left: auto;
    right: 0px;
    bottom: -20px;
    border: 12px solid;
    border-color: lightyellow lightyellow transparent transparent;
}

/* talk bubble contents */
.talktext{
  padding: 1em;
    text-align: right;
  line-height: 1.5em;
  /*position:absolute;
  bottom:0;
  right:0;*/
}
.talktext p{
  /* remove webkit p margins */
  -webkit-margin-before: 0em;
  -webkit-margin-after: 0em;
}

#wrapper{
        margin:0 auto;
        padding-bottom:25px;
        background:#EBF4FB;
        width:504px;
        border:1px solid #ACD8F0; }
        .welcome { float:left; }

        .logout { float:right; }
server.py

app = Flask(__name__)
socketio = SocketIO(app)

def fromConsumer(msg):
    print ("at from consumer")
    print(msg)
    socketio.emit('reply', msg)

@app.route("/")
def index():
    return render_template('index.html',)

@socketio.on('message')
def handleMessage(msg):

    print('Server got: ' + msg)
    replyData = "How are you"
    fromConsumer(replyData)


if __name__ == '__main__':
    socketio.run(app)
index.js

$(document).ready(function() {
  $('#wrapper').hide();
    var socket = io.connect('http://localhost:1000/');
    socket.on('connect', function() {
    console.log("connected")
    });
    socket.on('message', function(msg) {
        console.log('Received message');
    });
  socket.on('reply', function(msg) {
        console.log('Received reply');
    console.log(msg)
    $('#chatbox').append('<br>' + $('<div/>').text(msg).html());
    });
  $('#submitmsg').on('click', function() {
        socket.send($('#usermsg').val());
        $('#usermsg').val('');
    });
  $("#chatBubble").click(function(){
    $('#wrapper').show();
  })
});
$(文档).ready(函数(){
$(“#包装器”).hide();
var socket=io.connect('http://localhost:1000/');
socket.on('connect',function(){
控制台日志(“已连接”)
});
socket.on('message',函数(msg){
console.log(“收到的消息”);
});
socket.on('reply',函数(msg){
console.log('Received reply');
控制台日志(msg)
$('#聊天室').append('
'+$('').text(msg.html()); }); $('#submitmsg')。在('click',function()上{ send($('#usermsg').val()); $('#usermsg').val(''); }); $(“#聊天泡泡”)。单击(函数(){ $(“#包装器”).show(); }) });
如果您收到一个
msg
字符串,并确认您的代码。你能创建一个例子来帮助演示这个问题吗?它很好用。也许您应该先调试msg。@showdev添加的工作示例
肯定不是有效的HTML代码,div需要打开和关闭tag@showdev现在很好用。我需要清除浏览器缓存!
app = Flask(__name__)
socketio = SocketIO(app)

def fromConsumer(msg):
    print ("at from consumer")
    print(msg)
    socketio.emit('reply', msg)

@app.route("/")
def index():
    return render_template('index.html',)

@socketio.on('message')
def handleMessage(msg):

    print('Server got: ' + msg)
    replyData = "How are you"
    fromConsumer(replyData)


if __name__ == '__main__':
    socketio.run(app)
$(document).ready(function() {
  $('#wrapper').hide();
    var socket = io.connect('http://localhost:1000/');
    socket.on('connect', function() {
    console.log("connected")
    });
    socket.on('message', function(msg) {
        console.log('Received message');
    });
  socket.on('reply', function(msg) {
        console.log('Received reply');
    console.log(msg)
    $('#chatbox').append('<br>' + $('<div/>').text(msg).html());
    });
  $('#submitmsg').on('click', function() {
        socket.send($('#usermsg').val());
        $('#usermsg').val('');
    });
  $("#chatBubble").click(function(){
    $('#wrapper').show();
  })
});