html按钮问题,按钮不可用,无法点击,求解决!
<body><!------聊天框开始------><divclass="chatbox"><h1>欢迎使用默灬or惑网页实时聊天系统</h1><divclass="c_...
<body>
<!------聊天框开始------>
<div class="chatbox">
<h1>欢迎使用默灬or惑网页实时聊天系统</h1>
<div class="c_info">
服务器地址:<input type="text" class="inputText" style="width:360px" value="localhost:8080/mo_chat/moChat" id="addr"/>
用户名:<input type="text" class="inputText" id="username"/>
<input type="button" value="连接" class="btn"/>
</div>
<div class="c_message">
</div>
<div class="c_send">
<input class="inputText" id="sendtext" type="text" style="width:602px"/>
<input class="btn" type="button" id="sendbtn" value="发送">
</div>
</div>
<!------聊天框结束------>
<!------引入js官方类库jquery------>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
var connected = false;
var ws = null;
$(function(){
$(".c_send").hide();
//判断浏览器是否支持WebSocket
if(window.WebSocket || window.MozWebSocket){
printMsg("当前浏览器可以聊天,输入用户名后点击连接即可聊天!","OK");
}else{
printMsg("当前浏览器不兼容聊天,请更换浏览器!","ERROR");
//设置按钮不可点击
$("#con").attr("disabled","true");
}
});
//打印信息
function printMsg(msg,msgType){
if(msgType == "OK"){
msg = "<span style='color:#FFEB01;'>"+msg+"</span>";
}
if(msgType == "ERROR"){
msg = "<span style='color:red;'>"+msg+"</span>";
}
$(".c_message").append(msg+"<br/>");
};
//点击链接的时候触发事件
$("#con").click(function(){
if(connected){
//已连接
ws.send("{"+$("#username").val()+"}已下线!");
connected = false;
ws.close();
}else{
//没有链接
printMsg("正在准备连接服务器,请稍等!");
var url = "ws://"+$("#addr").val();
if("WebSocket" in window){
wss = new WebSocket(url);
}else if("MozWebSocket" in window){
ws = new MozWebSocket(url);
}
//链接成功后设置链接状态为true
connected = true;
$("#con").val("断开");
//注册事件
ws.onopen = openWs;
ws.onmessage = msgWs;
ws.onclose = closeWs;
ws.onerror = errorWs;
}
});
//打开socket
function openWs(){
printMsg("链接已建立!","OK");
ws.send("{"+$("#username").val()+"}已上线!");
$(".c_send").show();
}
//接收消息时
function msgWs(event){
printMsg(event.data);
}
//关闭链接
function closeWs(){
$("#con").val("连接");
//隐藏发送
$(".c_send").hide();
}
//产生错误
function errorWs(){
printMsg("与服务器连接错误","ERROR");
}
//点击发送
$("#sendbtn").click(function(){
var text = $("#sendtext").val();
ws.send($("#username").val()+": "+text);
$("#sendtext").val(" ");//清空发送框
});
</script>
<!------引入结束------>
</body>
</html>
按钮在不兼容websocket时可以点击但不触发任何事件,求大神解决! 展开
<!------聊天框开始------>
<div class="chatbox">
<h1>欢迎使用默灬or惑网页实时聊天系统</h1>
<div class="c_info">
服务器地址:<input type="text" class="inputText" style="width:360px" value="localhost:8080/mo_chat/moChat" id="addr"/>
用户名:<input type="text" class="inputText" id="username"/>
<input type="button" value="连接" class="btn"/>
</div>
<div class="c_message">
</div>
<div class="c_send">
<input class="inputText" id="sendtext" type="text" style="width:602px"/>
<input class="btn" type="button" id="sendbtn" value="发送">
</div>
</div>
<!------聊天框结束------>
<!------引入js官方类库jquery------>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
var connected = false;
var ws = null;
$(function(){
$(".c_send").hide();
//判断浏览器是否支持WebSocket
if(window.WebSocket || window.MozWebSocket){
printMsg("当前浏览器可以聊天,输入用户名后点击连接即可聊天!","OK");
}else{
printMsg("当前浏览器不兼容聊天,请更换浏览器!","ERROR");
//设置按钮不可点击
$("#con").attr("disabled","true");
}
});
//打印信息
function printMsg(msg,msgType){
if(msgType == "OK"){
msg = "<span style='color:#FFEB01;'>"+msg+"</span>";
}
if(msgType == "ERROR"){
msg = "<span style='color:red;'>"+msg+"</span>";
}
$(".c_message").append(msg+"<br/>");
};
//点击链接的时候触发事件
$("#con").click(function(){
if(connected){
//已连接
ws.send("{"+$("#username").val()+"}已下线!");
connected = false;
ws.close();
}else{
//没有链接
printMsg("正在准备连接服务器,请稍等!");
var url = "ws://"+$("#addr").val();
if("WebSocket" in window){
wss = new WebSocket(url);
}else if("MozWebSocket" in window){
ws = new MozWebSocket(url);
}
//链接成功后设置链接状态为true
connected = true;
$("#con").val("断开");
//注册事件
ws.onopen = openWs;
ws.onmessage = msgWs;
ws.onclose = closeWs;
ws.onerror = errorWs;
}
});
//打开socket
function openWs(){
printMsg("链接已建立!","OK");
ws.send("{"+$("#username").val()+"}已上线!");
$(".c_send").show();
}
//接收消息时
function msgWs(event){
printMsg(event.data);
}
//关闭链接
function closeWs(){
$("#con").val("连接");
//隐藏发送
$(".c_send").hide();
}
//产生错误
function errorWs(){
printMsg("与服务器连接错误","ERROR");
}
//点击发送
$("#sendbtn").click(function(){
var text = $("#sendtext").val();
ws.send($("#username").val()+": "+text);
$("#sendtext").val(" ");//清空发送框
});
</script>
<!------引入结束------>
</body>
</html>
按钮在不兼容websocket时可以点击但不触发任何事件,求大神解决! 展开
1个回答
2016-06-07
展开全部
printMsg("当前浏览器不兼容聊天,请更换浏览器!","ERROR");
//设置按钮不可点击
$("#con").attr("disabled","true");
//设置按钮不可点击
$("#con").attr("disabled","true");
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询