尝试使用鼠标点击事件响应编写jQuery HTML5聊天框程序

Trying to code jQuery HTML5 chat box program with mouse click event response

本文关键字:jQuery HTML5 聊天 程序 响应 事件 鼠标      更新时间:2023-09-26

我在为学校项目编写聊天框程序时遇到问题。我在该程序上遇到了 3 个问题。首先,我的聊天消息无法在我的聊天框中正确发布。其次,我希望能够单击退出并将其靠近窗口,但不完全确定该怎么做。最后,我希望聊天能够回显消息或至少具有硬编码响应。我正在研究如何做到这一点,我不太确定该怎么做。我有一个样式表和html。这是我的网页:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <script src=http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js ></script>
<link rel="stylesheet" type="text/css" href="jQuery.css">

//my issues seem to be in my Javascript between here************************************

<script type="text/javascript">
    $(document).ready(function(){
        $('#submitmsg').click(function(){
            var message = $('#usermsg').val()
            var old = $('#chatbox').html()
            $('#chatbox').html(old + '<p>' + message + '</p>');
        });
    });
</script>
<script >
    function close_window(url){
        var newWindow = window.open('', '_self', ''); //open the current window
        window.close(url);
    };
</script>

//and here*****************************************************************************
</head>
<body>
<div id="wrapper">
    <div id="menu">
        <p class="welcome">Welcome <b></b></p>
//Possible issue here*******************************************************************
        <p class="logout"><a id="exit" href="#" onclick="window.close();">Exit Chat</a></p>
        <div style="clear:both"></div>
    </div>
    <div id="chatbox">
        <p>Here's our chat data</p>
    </div>
    <form name="message" action="">
        <input name="usermsg" type="text" id="usermsg" size="63" />
        <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
    </form>
</div>      
</body>
</html>  

这是我的 css:

body {
font:12px arial;
color: #222;
text-align:center;
padding:35px; }
form, p, span {
    margin:0;
    padding:0; }
input { 
    font:12px arial; }
a {
    color:#0000FF;
    text-decoration:none; }
a:hover { 
    text-decoration:underline; }
#wrapper, #loginform {
    margin:0 auto;
    padding-bottom:25px;
    background:#EBF4FB;
    width:75%;
    border:1px solid #ACD8F0; }
#loginform { 
    padding-top:18px; }
#loginform p { 
    margin: 5px; }
#chatbox {
    text-align:left;
    margin:0 auto;
    margin-bottom:25px;
    padding:10px;
    background:#fff;
    height:270px;
    width:430px;
    border:1px solid #ACD8F0;
    overflow:auto; }
#chatbox p {
    padding:1em;
    margin:1em;
    background:#E6E6E6;
    border:1px solid #BDBDBD;
    -moz-border-radius:4px;
}
#usermsg {
    width:395px;
    border:1px solid #ACD8F0; }
#submit { 
    width: 60px; }

.welcome { 
    float:left; }
.logout { 
    float:right; }
.msgln { 
    margin:0 0 2px 0; }

我很确定有些 css 是不必要的,但是当我尝试取出一些时,它引起了问题。任何帮助将不胜感激。

尝试复制以下内容 - 我将样式放入头部,将javascript移动到末尾,并将输入换成按钮。这与 onclick 相关联,并将新评论附加到聊天窗口中。它可以工作,但可以做得更好,我也没有解决关闭窗口的问题,但这确实将新评论发布到聊天区域。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" ></script>
<style>
body {
font:12px arial;
color: #222;
text-align:center;
padding:35px; }
form, p, span {
    margin:0;
    padding:0; }
input { 
    font:12px arial; }
a {
    color:#0000FF;
    text-decoration:none; }
a:hover { 
    text-decoration:underline; }
#wrapper, #loginform {
    margin:0 auto;
    padding-bottom:25px;
    background:#EBF4FB;
    width:75%;
    border:1px solid #ACD8F0; }
#loginform { 
    padding-top:18px; }
#loginform p { 
    margin: 5px; }
#chatbox {
    text-align:left;
    margin:0 auto;
    margin-bottom:25px;
    padding:10px;
    background:#fff;
    height:270px;
    width:430px;
    border:1px solid #ACD8F0;
    overflow:auto; }
#chatbox p {
    padding:1em;
    margin:1em;
    background:#E6E6E6;
    border:1px solid #BDBDBD;
    -moz-border-radius:4px;
}
#usermsg {
    width:395px;
    border:1px solid #ACD8F0; }
#submit { 
    width: 60px; }

.welcome { 
    float:left; }
.logout { 
    float:right; }
.msgln { 
    margin:0 0 2px 0; }
    </style>
</head>
<body>
<div id="wrapper">
    <div id="menu">
        <p class="welcome">Welcome</p>
        <p class="logout"><a id="exit" href="#" onclick="window.close();">Exit Chat</a></p>
        <div style="clear:both"></div>
    </div>
    <div id="chatbox">
        <p>Here's our chat data</p>
    </div>
    <form name="message">
        <input name="usermsg" type="text" id="usermsg" size="63" />
        <button type="button" id="submitmsg" value="send">Send</button>    
    </form>
</div> 
<script>
    $(document).ready(function(){
        $('#submitmsg').click(function(){
            var message = $('#usermsg').val();
            $('#chatbox').append('<p>' + message + '</p>');
            $('#usermsg').val('');
        });
    });
 function close_window(url){
        var newWindow = window.open('', '_self', ''); //open the current window
        window.close(url);
    };
</script>
</body>
</html> 

一个问题是你正在使用一个表单,然后发布到同一页面 - 重新加载它。我将您的代码复制到测试页面(称为chatTest.html)中,并在点击发送按钮后得到以下网址:

chatTest.html?usermsg=test+message&submitmsg=Send

您的消息确实会发布到聊天部分,但会在页面重新加载时被删除。 并且由于您没有指定方法 - 它将其视为获取表单并将消息作为附加到 URL 的查询字符串发布。

如果您打算将注释保存到数据库,那么您应该将表单的操作更改为不同的 PHP 页面,处理结果并将保存的内容返回到聊天部分 - 也许使用 Ajax,这样您就不必在每次聊天提交时重新加载页面。