JS适用于IE,Safari,Chrome,但不适用于Firefox?

时间:2013-07-26 20:19:08

标签: javascript internet-explorer google-chrome firefox

我的大多数工作都是在Webkit上运行并使用Chrome的开发工具完成的。但是这段代码不能在Firefox中运行;它甚至没有抛出控制台错误,所以我不知道出了什么问题。有任何想法吗。我确信它非常容易。我的代码旨在为特定语法“t_”“r_”抛出div,它们是对外部文档的引用;我正在尝试找到所有所述引用并用超链接替换它们。我是JS编程员的新手,对于你在这里无疑会看到的幼稚感到抱歉。

(我正在使用FF 22.0)

<!DOCTYPE html>
<html>
<head>
<script>
function pre_forwarder(){ //First Step
patt_1=/\[r_/g;
patt_2=/\[t_/g;
patt_S_and_R_1 = "[r_";
patt_S_and_R_2 = "[t_";
forwarder(patt_1,patt_S_and_R_1);
forwarder(patt_2,patt_S_and_R_2);
}
function forwarder(Pattern,Pre_SearchReplace){
rule_string = document.getElementById("divid");
rule_string = rule_string.innerText;
var patt1 = Pattern;
while (patt1.test(rule_string)==true) 
      {
      begin_string = patt1.lastIndex;
      fullpar_string = patt1.lastIndex + 5;
      partial_string = rule_string.slice(begin_string,fullpar_string); //5 characters
      //alert("partial_string-"+partial_string);
      refined_string_end = partial_string.indexOf("]");
      refined_str = partial_string.slice(0,refined_string_end); //raw number
      full_str = Pre_SearchReplace+refined_str+"]"; //restructured
      SectionNum = refined_str;
      SearchReplace = full_str; //Variable to pass to parse()
      //alert("S&R="+SearchReplace+" >> full_string-"+full_str);
      //alert("S&R"+SearchReplace+">>SecNum-"+SectionNum+">>Pre-Search-"+Pre_SearchReplace);
      Parse(SearchReplace,SectionNum,Pre_SearchReplace);
      }
 //var patt_end=/\[t_/g;
 }
function Parse(SearchReplace,SectionNum,Pre_SearchReplace){
if (Pre_SearchReplace == patt_S_and_R_1){
ref_URL = "UEB_Ref.html#";
}else if (Pre_SearchReplace == patt_S_and_R_2){
ref_URL = "UEB_Tech.html#";
}
linkCreate = '<a href="javascript:Dude(\''+ref_URL+SectionNum+'\');">Link to ch-'+SectionNum+'</a> ';
document.getElementById("divid").innerHTML = document.getElementById("divid").innerHTML.replace(SearchReplace, linkCreate);
}
function Dude(SectionNum){
alert(SectionNum);
}
</script>
</head>
<body onload="pre_forwarder();">
<button onclick="pre_forwarder();">
New Tool</button>
<div id="divid">
hello [dude] ok the end [r_12.1][r_7] [r_22]
<br>bro try this [t_15.3][t_6] [t_5.5]
</div>
<div id="hyperlink">
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

你在第15行rule_string = rule_string.innerText;。FF不知道innerText,您需要使用textContent,或者修改如下:

rule_string = rule_string.innerText || rule_string.textContent;

A live demo at jsFiddle


作为旁注:看起来您的应用程序基于全局变量。请不要使用这种编程技术。只要代码不多,它就可以完成这项工作,但是当你的应用程序变得越来越大时,你将失去对大量全局变量的控制。

首先,您可以在MDN阅读JavaScript Guide,尤其是第4,8和9章。