无需混合使用 jquery 和 vanila 脚本事件处理程序
$(document).ready(function() {
var limit = 15;
var $counter = $('#text_counter').text(limit);
var $input = $('#status').keyup(function() {
var left = limit - $(this).val().length;
$counter.toggleClass("overlimit", left < 0).text('Characters left: ' + left);
});
$('button').click(function() {
var val = $input.val();
if (val.length < 10) {
alert("Post must be at least 10 characters.");
return;
}
$('table').append('<tr><td>' + val + '</td></tr>');
$input.val('');
$counter.text(limit).removeClass('overlimit');
})
});
$(document).ready(function() {
var limit = 15;
var $counter = $('#text_counter').text(limit);
var $input = $('#status').keyup(function() {
var left = limit - $(this).val().length;
$counter.toggleClass("overlimit", left < 0).text('Characters left: ' + left);
});
$('button').click(function() {
var val = $input.val();
if (val.length < 10) {
alert("Post must be at least 10 characters.");
return;
}
$('table').append('<tr><td>' + val + '</td></tr>');
$input.val('');
$counter.text(limit).removeClass('overlimit');
})
});
input[type=text] {
padding: 5px;
border: 2px solid #000080;
-webkit-border-radius: 5px;
border-radius: 5px;
}
input[type=text]:focus {
border-color: #ccc;
}
-webkit-border-radius:5px;
border-radius:5px;
}
.rows {
text-align: center;
}
.postTable {
width: 400px;
height: 33px;
border: solid 1px lightgray;
border-width: 2px;
font-family: Verdana;
font-size: 20;
}
td {
border-bottom: solid 2px lightgray;
}
tr:last-child td {
border: none!important;
}
.overlimit {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="postBox">
<center>
<input type="text" id="status" name="post" maxlength="150" /> <span id="text_counter"></span>
<br>
<br/>
<button style="border : solid 0px #000080; border-radius : 4px; moz-border-radius : 4px; -webkit-box-shadow : 0px 0px 5px rgba(0, 0, 0, 1.0); -moz-box-shadow : 0px 0px 5px rgba(0,0,0,1.0); box-shadow : 0px 0px 5px rgba(0,0,0,1.0); font-size : 24px; font-style : ;color : #ffffff; padding : 4px 10px; background-color : #000080;">subpost</button>
<br/>
<br/>
<table name="rows" class="postTable"></table>
<td></td>
<tr></tr>
</center>
</div>