我想在提交后将输入字段的值加粗。这是我使用 .bold() 时得到的结果
http://puu.sh/j4e35/b77ea3f62e.png
为什么要这样做以及如何加粗它?
<body>
<h1 id="header">
Socket.IO Chat
</h1>
<div id="message-box">
<ul id="messages"></ul>
</div>
<div id="chat-box">
<form action="" id="username-form" align="center">
<input placeholder="Enter your nickname" autocomplete="off" id="nickname">
</form>
<form action="" id="client-input" align="center">
<input placeholder="Press enter to send a message..." autocomplete="off" id="m">
</form>
</div>
<script>
var socket = io();
var nickname;
$(document).ready(function(){
$('#client-input').hide();
$('#username-form').submit(function(){
$('#nickname').hide();
$('#client-input').show();
return false;
});
});
$('#client-input').submit(function(){
if($('#m').val().trim().length == 0){
null;
}
else{
socket.emit('chat message',$('#nickname').val().bold() + ': ' + $('#m').val());
$('#m').val('');
}
return false;
});
socket.on('chat message', function(msg){
$('#messages').prepend($('<li>').text(msg));
});
</script>
最佳答案
.bold()
方法实际上并没有使文本变为粗体,它只是将 <b>
给定字符串周围的标签。在输入字段中,您实际上会在字符串周围看到这些标签,因为它是预先格式化的。
而是使用 JavaScript 更改输入字段的 CSS 以粗体显示其内容:
$('#nickname').css('font-weight', 'bold');
关于javascript - 加粗输入字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31496449/