javascript - 'textContent' is null or not an object - Stack Overflow

i have a problem with this codethis code run great on all browsers exept IE,my IE is 8.0any solution?

i have a problem with this code this code run great on all browsers exept IE,my IE is 8.0 any solution? i won't using jquery sincerely Note: i changed Node.TEXT_NODE to 3 but other error occured:'textContent' is null or not an object

 <!DOCTYPE html>
 <html>
 <head>
 <script>

 function replaceText(oldText, newText, node){ 
   node = node || document.body; 

   var childs = node.childNodes, i = 0;

   while(node = childs[i]){ 
     if (node.nodeType == Node.TEXT_NODE){ 
  node.textContent = node.textContent.replace(oldText, newText); 
} else { 
  replaceText(oldText, newText, node); 
} 
     i++; 
   } 
 }

 </script>
 </head>
 <body>
 old
 <h1 id="myHeader" onclick="replaceText('old','new')">old Click me! whatever</h1>

 </body>
 </html>

i found it

   while(i < childs.length){
   if (rgx.test(document.body.innerHTML)){
   childs[i][textPropName] =childs[i][textPropName].replace(rgx,'new');
   }
   else
   {
   replaceText(oldText, newText,document.body.childNodes[i])
   }
        i++; 
      } 
    }  

i have a problem with this code this code run great on all browsers exept IE,my IE is 8.0 any solution? i won't using jquery sincerely Note: i changed Node.TEXT_NODE to 3 but other error occured:'textContent' is null or not an object

 <!DOCTYPE html>
 <html>
 <head>
 <script>

 function replaceText(oldText, newText, node){ 
   node = node || document.body; 

   var childs = node.childNodes, i = 0;

   while(node = childs[i]){ 
     if (node.nodeType == Node.TEXT_NODE){ 
  node.textContent = node.textContent.replace(oldText, newText); 
} else { 
  replaceText(oldText, newText, node); 
} 
     i++; 
   } 
 }

 </script>
 </head>
 <body>
 old
 <h1 id="myHeader" onclick="replaceText('old','new')">old Click me! whatever</h1>

 </body>
 </html>

i found it

   while(i < childs.length){
   if (rgx.test(document.body.innerHTML)){
   childs[i][textPropName] =childs[i][textPropName].replace(rgx,'new');
   }
   else
   {
   replaceText(oldText, newText,document.body.childNodes[i])
   }
        i++; 
      } 
    }  
Share Improve this question edited Jan 27, 2013 at 10:43 MD66 asked Jan 27, 2013 at 4:14 MD66MD66 1013 silver badges11 bronze badges 2
  • @HovercraftFullOfEels - I've corrected that. – Stephen C Commented Jan 27, 2013 at 4:25
  • i'm sory i use space for java script then forget to correct it after edit – MD66 Commented Jan 27, 2013 at 4:28
Add a ment  | 

2 Answers 2

Reset to default 3

Node.TEXT_NODE & textContent are not available in IE8.


Use 3 instead of Node.TEXT_NODE, and use innerText if textContent is not available:

var textPropName = node.textContent === undefined ? 'innerText' : 'textContent';

if (node.nodeType == 3) {
    node[textPropName] = node[textPropName].replace(oldText, newText); 
} else { 
    replaceText(oldText, newText, node); 
}

You should probably cache that textPropName outside your function, so that you don't recheck it every single time the function is called (use document.body for the test).

instead of textContent, use nodeValue

node.nodeValue = node.nodeValue.replace(oldText, newText); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745088284a4610537.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信