2013-07-26 216 views
0

我的大部分工作都是在Webkit上运行并使用Chrome的开发工具完成的。但是这段代码不能在Firefox中运行;它甚至不会抛出控制台错误,所以我不知道什么是错的。有任何想法吗。我相信它非常容易。我的代码旨在为特定的语法“t_”“r_”引入外部文档;我试图找到所有说的引用,并用超链接替换它们。我“马新手JS编码器,用于你在这里无疑看到的naiveness非常抱歉。JS适用于IE,Safari,Chrome,但不适用于Firefox?

(我使用的是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> 
+2

这需要1)格式和2)小提琴 – BLSully

回答

1

rule_string = rule_string.innerText;在15行FF不知道innerText,你需要使用textContent,或像修复:

rule_string = rule_string.innerText || rule_string.textContent; 

A live demo at jsFiddle


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

对于初学者来说,你可以阅读JavaScript Guide在MDN,特别是第4章,第8和9

+0

感谢都行;我是一个新手,通过一切手段。 – CertDoctor

+0

@CertDoctor如果你可以使用答案,你也可以通过勾选答案左上角的复选标记来接受答案。 – Teemu

相关问题