0

私の仕事のほとんどは、Webkit で実行し、Chrome の開発ツールを使用して行われます。しかし、このコードは Firefox では動作しません。コンソールエラーさえスローしないので、何が問題なのかわかりません。何か案は。私はそれがひどく簡単だと確信しています。私のコードは、外部ドキュメントへの参照である特定の構文「t_」「r_」の div をスローするように設計されています。上記のすべての参照を見つけて、ハイパーリンクに置き換えようとしています。私は初心者の JS コーダーです。

(FF22.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>
4

1 に答える 1

1

15 行目です。FFrule_string = rule_string.innerText;は を認識していません。innerText使用する必要がありますtextContent

rule_string = rule_string.innerText || rule_string.textContent;

jsFiddle でのライブ デモ


補足として: あなたのアプリはグローバル変数に基づいているようです。この種のプログラミング手法は使用しないでください。コードがあまりない限り、これで十分ですが、アプリが大きくなると、大量のグローバル変数を制御できなくなります。

手始めに、 MDN のJavaScript ガイド、特に第 4、8、9 章を読むことができます。

于 2013-07-26T20:39:53.950 に答える