日期:2014-05-16  浏览次数:20396 次

关于js ff浏览器兼容innerText的问题
<html>
 <head>
   <script 
language="javascript">
     function test(){
        var 
str=$('mywrite').innerText||$('mywrite').textContent; 
       var 
reg="/"+$('text1').value+"/gi";
       var rep=$('text2').value;   
//字串转对象
       var newstr=str.replace(eval(reg),rep)     
       
if($('computer').innerText){
        $('computer').innerText=newstr;  
 
        }else if($('computer').textContent){
      
       $('computer').textContent=newstr;
       }

}
      function $(id){
       return document.getElementById(id);     
 
      }
   
</script>
 </head>
 <body>
  <h1>替换小测试</h1>
  <textarea 
cols="30" rows="7" id="mywrite">
   </textarea>
  <textarea 
cols="30" rows="7" id="computer">
   </textarea><br/>
   
请输入您要查找字串<input type="text" id="text1"/><br/>
   
请输入您要替换成的字串<input type="text" id="text2"/><br/>
   <input 
type="button" id="but1" value="开始替换" 
onclick="test()"/>
 </body>
</html>

上面的textContent;在ff,和chrome不起作用?是哪里错了?

------解决方案--------------------
<html>
<head>
<script language="javascript">
function test(){
var str=$('mywrite').value ;

var reg="/"+$('text1').value+"/gi";
var rep=$('text2').value;   
//字串转对象
var newstr=str.replace(eval(reg),rep)     

$('computer').value=newstr;  

}
function $(id){
return document.getElementById(id);     

}

</script>
</head>
<body>
<h1>替换小测试</h1>
<textarea cols="30" rows="7" id="mywrite"></textarea>
<textarea cols="30" rows="7" id="computer"></textarea><br/>

请输入您要查找字串<input type="text" id="text1"/><br/>

请输入您要替换成的字串<input type="text" id="text2"/><br/>
<input type="button" id="but1" value="开始替换" onclick="test()"/>
</body>
</html>