我做的是一个在输入框输入字的时候同时显示输入了多少个字,在IE下运行正常,但到firefox下就死掉了,代码是这样的
<html>
<head> <title>jishu</title>
<script language="javascript">
funtion check(strInput)
{
var num=0;
var reg=/\b/g; while(reg.exec(strInput))
num++; if(navigator.appName.indexOf("Explorer") >-1)
{
document.getElementById('txt').innerText = num/2;
}
else
{
document.getElementById('txt').textContent = num/2;
} }
</script>
</head>
<input id="txt" >
<body >
<textarea cols="15" rows="25" onkeydown="check(this.value)" onblur="chec(this.value)"></textarea>
<input type="text" value="" id="txt">
</body>
</html>
<html>
<head> <title>jishu</title>
<script language="javascript">
funtion check(strInput)
{
var num=0;
var reg=/\b/g; while(reg.exec(strInput))
num++; if(navigator.appName.indexOf("Explorer") >-1)
{
document.getElementById('txt').innerText = num/2;
}
else
{
document.getElementById('txt').textContent = num/2;
} }
</script>
</head>
<input id="txt" >
<body >
<textarea cols="15" rows="25" onkeydown="check(this.value)" onblur="chec(this.value)"></textarea>
<input type="text" value="" id="txt">
</body>
</html>
{
document.getElementById('txt').value= num/2;
}
else
{
document.getElementById('txt').value= num/2;
} }
<head> <title>jishu </title>
<script language="javascript">
function check(str)
{
$("show").innerHTML = str.length
}
function $(id)
{
return document.getElementById(id);
}
</script>
</head>
<body >
<textarea id="txt" cols="15" rows="25" onkeyup="check($('txt').value)" onblur="check($('txt').value)"></textarea>
<div id="show"></div>
</body>
</html>
改为:document.getElementById('txt').innerHTML = num/2;
innerText在firefox中无效
测试环境:
win xp sp3
ie6
firefox 2.0.0.14
代码如下修改完毕。 <html>
<head> <title>jishu </title>
<script language="javascript">
function check(myObj)//strInput)
{
var num=0;
var reg=/\b/g;
var strInput=myObj.value;
/*
while(reg.exec(strInput))
{
//strInput=RegExp.rightContext;
num++;
}
*/
var arrMatches =strInput.match(reg);
num=arrMatches.length;
document.getElementById('txt').value=num/2;
/*
if(navigator.appName.indexOf("Explorer") >-1)
{
document.getElementById('txt').innerText = num/2;
}
else
{
document.getElementById('txt').textText = num/2;
}
*/
return; }
</script>
</head>
<body >
<textarea cols="15" rows="25" onkeyup="javascript:check(this);" onblur="javascript:check(this);"></textarea>
<input type="text" value="" id="txt" />
</body>
</html>
可能是死循环了。
{
document.getElementById('txt').innerHTML=strInput.value.split(/\s+/).length;
}
测试通过 over