[英]JS works in IE, Safari, Chrome but not in 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>
您已经rule_string = rule_string.innerText;
在第15行innerText
不知道innerText
,您需要使用textContent
或类似的修补程序:
rule_string = rule_string.innerText || rule_string.textContent;
附带说明:看来您的应用基于全局变量。 请不要使用这种编程技术。 只要没有太多代码,它就可以完成工作,但是当您的应用程序变得越来越大时,您将失去对大量全局变量的控制。
首先,您可以阅读MDN上的JavaScript指南 ,尤其是第4、8和9章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.