限制字符串长度的问题 就10个字符还不如用input 设置最大长度<input type="text" maxlength="10" size="10"> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <input type="text" maxlength="10" size="10"> 楼主,textarea没有value属性吧?<html><head> <title></title> <script type="text/javascript"> function t() { var obj = document.getElementById("obj1"); if (obj.value.length > 10) { obj.value = obj.value.substr(0,10); } } </script></head><body> <input id="obj1" onchange="t()" onkeydown="t()" type="text" /></body></html> 从discuz中提取的:<textarea id="message" name="message" onkeyup="textCounter(this, 'maxlimit', 200)" onkeydown="ctrlEnter(event, 'add');" rows="4" style="width: 460px; height: 5em;"></textarea>function textCounter(obj, showid, maxlimit) { var len = strLen(obj.value); var showobj = $(showid); if(len > maxlimit) { obj.value = getStrbylen(obj.value, maxlimit); showobj.innerHTML = '0'; } else { showobj.innerHTML = maxlimit - len; } if(maxlimit - len > 0) { showobj.parentNode.style.color = ""; } else { showobj.parentNode.style.color = "red"; } } function ctrlEnter(event, btnId, onlyEnter) { if(isUndefined(onlyEnter)) onlyEnter = 0; if((event.ctrlKey || onlyEnter) && event.keyCode == 13) { $(btnId).click(); }}CSDN的个人主页的迷你博客中也有的!下载下来应该能看明白吧! 人家问的是textarea,不是input,刚刚用扣来的代码+prototype1.6作了一个demo:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><script type="text/javascript" src="http://localhost/demo/include/js/prototype.js"></script><title>无标题文档</title><script type="text/javascript">function strLen(str) { var charset = document.charset; var len = 0; for(var i = 0; i < str.length; i++) { len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (charset == "utf-8" ? 3 : 2) : 1; } return len;}function isUndefined(variable) { return typeof variable == 'undefined' ? true : false;}function textCounter(obj, showid, maxlimit) { var len = strLen(obj.value); var showobj = $(showid); if(len > maxlimit) { obj.value = getStrbylen(obj.value, maxlimit); showobj.innerHTML = '0'; } else { showobj.innerHTML = maxlimit - len; } if(maxlimit - len > 0) { showobj.parentNode.style.color = ""; } else { showobj.parentNode.style.color = "red"; }}</script></head><body><div id="sitepage"> <div id="recordlist"> <dl> <dt>Write a Review</dt> <dd><textarea name="content" rows="5" cols="30" onkeyup="textCounter(this, 'mesg', 200)" onkeydown="ctrlEnter(event, 'add');"></textarea></dd> <dd><label id="mesg">200</label></dd> </dl> </div></div></body></html> 把onkeydown拿掉!哪个函数我在demo中没写上 看这就知有没有了:http://topic.csdn.net/t/20000315/14/4719.html 这招太狠...我研究研究xiaofanku说的~ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script type="text/javascript">function textLenCheck(obj, maxlimit) { var Str = obj.innerText; var len = Str.length; if(len < maxlimit) { return true; } else { return false; }}</script></head><body><div id="sitepage"> <div id="recordlist"> <dl> <dt>Write a Review</dt> <dd><textarea name="content" rows="5" cols="30" onkeydown="return textLenCheck(this,10);"></textarea></dd> </dl> </div></div></body></html> 怎麼拆分字符串 比如說。。。。。。。。。。。。。。。 如何判断链接内容是否有效?链接的内容较大 如何用JavaScript下载和保存文件? |zyciis| 如何得到 json 的度 js赋值<a>标签 寻求JS实现随机打乱数组的最佳方案 请教各位高手一个问题 oPopup.show 窗口 怎样 互相传递参数 怎么在JavaScript中设置iframe的滚动条? ext combo 点击文本框也会出现下拉 求一个树结构像csdn里面的那样 要动态的 ff 超连接的onclick不响应
<head>
<title></title>
<script type="text/javascript"> function t()
{
var obj = document.getElementById("obj1");
if (obj.value.length > 10)
{
obj.value = obj.value.substr(0,10);
}
}
</script>
</head>
<body>
<input id="obj1" onchange="t()" onkeydown="t()" type="text" />
</body>
</html>
<textarea id="message" name="message" onkeyup="textCounter(this, 'maxlimit', 200)" onkeydown="ctrlEnter(event, 'add');" rows="4" style="width: 460px; height: 5em;"></textarea>function textCounter(obj, showid, maxlimit) { var len = strLen(obj.value); var showobj = $(showid); if(len > maxlimit) { obj.value = getStrbylen(obj.value, maxlimit); showobj.innerHTML = '0'; } else { showobj.innerHTML = maxlimit - len; } if(maxlimit - len > 0) { showobj.parentNode.style.color = ""; } else { showobj.parentNode.style.color = "red"; } }
function ctrlEnter(event, btnId, onlyEnter) { if(isUndefined(onlyEnter)) onlyEnter = 0; if((event.ctrlKey || onlyEnter) && event.keyCode == 13) { $(btnId).click(); }}CSDN的个人主页的迷你博客中也有的!下载下来应该能看明白吧!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript" src="http://localhost/demo/include/js/prototype.js"></script>
<title>无标题文档</title>
<script type="text/javascript">
function strLen(str) {
var charset = document.charset;
var len = 0;
for(var i = 0; i < str.length; i++) {
len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (charset == "utf-8" ? 3 : 2) : 1;
}
return len;
}
function isUndefined(variable) {
return typeof variable == 'undefined' ? true : false;
}
function textCounter(obj, showid, maxlimit) {
var len = strLen(obj.value);
var showobj = $(showid);
if(len > maxlimit) {
obj.value = getStrbylen(obj.value, maxlimit);
showobj.innerHTML = '0';
} else {
showobj.innerHTML = maxlimit - len;
}
if(maxlimit - len > 0) {
showobj.parentNode.style.color = "";
} else {
showobj.parentNode.style.color = "red";
}
}
</script>
</head><body>
<div id="sitepage">
<div id="recordlist">
<dl>
<dt>Write a Review</dt>
<dd><textarea name="content" rows="5" cols="30" onkeyup="textCounter(this, 'mesg', 200)" onkeydown="ctrlEnter(event, 'add');"></textarea></dd>
<dd><label id="mesg">200</label></dd>
</dl>
</div>
</div>
</body>
</html>
看这就知有没有了:http://topic.csdn.net/t/20000315/14/4719.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function textLenCheck(obj, maxlimit) {
var Str = obj.innerText;
var len = Str.length;
if(len < maxlimit) {
return true;
} else {
return false;
}
}</script>
</head><body>
<div id="sitepage">
<div id="recordlist">
<dl>
<dt>Write a Review</dt>
<dd><textarea name="content" rows="5" cols="30" onkeydown="return textLenCheck(this,10);"></textarea></dd>
</dl>
</div>
</div>
</body>
</html>