Javascript jQuery$(this).html(text)不';I don’我不在客栈工作

Javascript jQuery$(this).html(text)不';I don’我不在客栈工作,javascript,jquery,html,replace,Javascript,Jquery,Html,Replace,我正在编写一个脚本,可以更改文本以显示用户选择的语言。 实际上,我是在页面加载时做的,默认情况下使用ita语言。它适用于所有div,除了innested div。我不明白为什么,它不会引发异常,所以try-catch都不能告诉我为什么。这是我的剧本: var pattEng1 = /\[lang='eng']/g; var pattEng2 = /\[.{1}lang='eng']/g; var pattIta1 = /\[lang='ita']/g; var pattIta2 = /\[.{1

我正在编写一个脚本,可以更改文本以显示用户选择的语言。
实际上,我是在页面加载时做的,默认情况下使用ita语言。它适用于所有div,除了innested div。我不明白为什么,它不会引发异常,所以try-catch都不能告诉我为什么。这是我的剧本:

var pattEng1 = /\[lang='eng']/g;
var pattEng2 = /\[.{1}lang='eng']/g;
var pattIta1 = /\[lang='ita']/g;
var pattIta2 = /\[.{1}lang='ita']/g;
var langs = ["ita","eng"];
var texts = new Array();
var i = 0, j;
var textToEdit, startPosEng, endPosEng;

$("div").each(function(){
    texts.push($(this).html());
    textToEdit =  texts[i];
    startPosEng = textToEdit.search(pattEng1) + 12;
    endPosEng = textToEdit.search(pattEng2) - startPosEng;
    textToEdit = textToEdit.replace(textToEdit.substr(startPosEng,endPosEng),"");
    try {
        $(this).html(textToEdit);
    }
    catch(err) {
       $("#demo").html(err.message);
    }
    j = 0;
    while(j < langs.length) {
        textToEdit = $(this).html();
        textToEdit = textToEdit.replace("[lang='"+langs[j]+"']", "");
        textToEdit = textToEdit.replace("[\/lang='"+langs[j]+"']", "")
        $(this).html(textToEdit);
        j++;
    }
    i++;
});
var pattEng1=/\[lang='eng']/g;
var pattEng2=/\[.{1}lang='eng']/g;
var pattIta1=/\[lang='ita']/g;
var pattIta2=/\[.{1}lang='ita']/g;
变量langs=[“ita”,“eng”];
var text=新数组();
var i=0,j;
var textToEdit、startPosEng、endPosEng;
$(“div”)。每个(函数(){
text.push($(this.html());
textToEdit=文本[i];
startPosEng=textToEdit.search(模式1)+12;
endPosEng=textToEdit.search(模式2)-startPosEng;
textToEdit=textToEdit.replace(textToEdit.substr(startposen,endPosEng),“”);
试一试{
$(this.html(textToEdit);
}
捕捉(错误){
$(“#demo”).html(err.message);
}
j=0;
而(j
我尝试在innested div中的“replace”之后“alert”textToEdit变量,它工作了,它替换了它必须替换的内容,但它没有将其放入$(this).html(textToEdit)。
非常感谢你。 下面是我的HTML代码:

<div>
[lang='eng']
CHAPTER ONE - 01<br>
THE BOY WHO LIVED<br>
Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say
that they were perfectly normal, thank you very much. They were the last
people you'd expect to be involved in anything strange or mysterious,
because they just didn't hold with such nonsense.<br>
Mr. Dursley was the director of a firm called Grunnings, which made
drills. He was a big, beefy man with hardly any neck, although he did
have a very large mustache. Mrs. Dursley was thin and blonde and had
nearly twice the usual amount of neck, which came in very useful as she
spent so much of her time craning over garden fences, spying on the
neighbors. The Dursleys had a small son called Dudley and in their
opinion there was no finer boy anywhere.<br>
The Dursleys had everything they wanted, but they also had a secret, and
their greatest fear was that somebody would discover it. They didn't
think they could bear it if anyone found out about the Potters. Mrs.
Potter was Mrs. Dursley's sister, but they hadn't met for several years;
in fact, Mrs. Dursley pretended she didn't have a sister, because her
sister and her good-for-nothing husband were as unDursleyish as it was
possible to be. The Dursleys shuddered to think what the neighbors would
say if the Potters arrived in the street. The Dursleys knew that the
Potters had a small son, too, but they had never even seen him. This boy
was another good reason for keeping the Potters away; they didn't want
Dudley mixing with a child like that.[/lang='eng']
[lang='ita']
CAPITOLO UNO - 01<br>
IL RAGAZZO SOPRAVVISSUTO<br>
Mr e Mrs Dursley, di Privet Drive numero 4, erano orgogliosi di poter affermare che erano
perfettamente normali e grazie tante. Erano le ultime persone al mondo da cui aspettarsi che
avessero a che fare con cose strane o misteriose, perché sciocchezze del genere proprio non le approvavano.<br>
Mr Dursley era direttore di una ditta di nome Grunnings, che fabbricava trapani. Era un uomo
corpulento, nerboruto, quasi senza collo e con un grosso paio di baffi. Mrs Dursley era magra,
bionda e con un collo quasi due volte più lungo del normale, il che le tornava assai utile, dato che
passava gran parte del tempo ad allungarlo oltre la siepe del giardino per spiare i vicini.
I Dursley avevano un figlioletto di nome Dudley e secondo loro non esisteva al mondo un bambino più bello.[/lang='ita']
<div>
    [lang='eng']
    CHAPTER ONE - 01b<br>
    THE BOY WHO LIVED<br>
    Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say
    that they were perfectly normal, thank you very much. They were the last
    people you'd expect to be involved in anything strange or mysterious,
    because they just didn't hold with such nonsense.<br>
    Mr. Dursley was the director of a firm called Grunnings, which made
    drills. He was a big, beefy man with hardly any neck, although he did
    have a very large mustache. Mrs. Dursley was thin and blonde and had
    nearly twice the usual amount of neck, which came in very useful as she
    spent so much of her time craning over garden fences, spying on the
    neighbors. The Dursleys had a small son called Dudley and in their
    opinion there was no finer boy anywhere.<br>
    The Dursleys had everything they wanted, but they also had a secret, and
    their greatest fear was that somebody would discover it. They didn't
    think they could bear it if anyone found out about the Potters. Mrs.
    Potter was Mrs. Dursley's sister, but they hadn't met for several years;
    in fact, Mrs. Dursley pretended she didn't have a sister, because her
    sister and her good-for-nothing husband were as unDursleyish as it was
    possible to be. The Dursleys shuddered to think what the neighbors would
    say if the Potters arrived in the street. The Dursleys knew that the
    Potters had a small son, too, but they had never even seen him. This boy
    was another good reason for keeping the Potters away; they didn't want
    Dudley mixing with a child like that.[/lang='eng']
    [lang='ita']
    CAPITOLO UNO - 01b<br>
    IL RAGAZZO SOPRAVVISSUTO<br>
    Mr e Mrs Dursley, di Privet Drive numero 4, erano orgogliosi di poter affermare che erano
    perfettamente normali e grazie tante. Erano le ultime persone al mondo da cui aspettarsi che
    avessero a che fare con cose strane o misteriose, perché sciocchezze del genere proprio non le approvavano.<br>
    Mr Dursley era direttore di una ditta di nome Grunnings, che fabbricava trapani. Era un uomo
    corpulento, nerboruto, quasi senza collo e con un grosso paio di baffi. Mrs Dursley era magra,
    bionda e con un collo quasi due volte più lungo del normale, il che le tornava assai utile, dato che
    passava gran parte del tempo ad allungarlo oltre la siepe del giardino per spiare i vicini.
    I Dursley avevano un figlioletto di nome Dudley e secondo loro non esisteva al mondo un bambino più bello.[/lang='ita']
</div>
</div>
<br><br><br><br>
<div>
    [lang='eng']
    CHAPTER ONE - 03<br>
    THE BOY WHO LIVED<br>
    Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say
    that they were perfectly normal, thank you very much. They were the last
    people you'd expect to be involved in anything strange or mysterious,
    because they just didn't hold with such nonsense.<br>
    Mr. Dursley was the director of a firm called Grunnings, which made
    drills. He was a big, beefy man with hardly any neck, although he did
    have a very large mustache. Mrs. Dursley was thin and blonde and had
    nearly twice the usual amount of neck, which came in very useful as she
    spent so much of her time craning over garden fences, spying on the
    neighbors. The Dursleys had a small son called Dudley and in their
    opinion there was no finer boy anywhere.<br>
    The Dursleys had everything they wanted, but they also had a secret, and
    their greatest fear was that somebody would discover it. They didn't
    think they could bear it if anyone found out about the Potters. Mrs.
    Potter was Mrs. Dursley's sister, but they hadn't met for several years;
    in fact, Mrs. Dursley pretended she didn't have a sister, because her
    sister and her good-for-nothing husband were as unDursleyish as it was
    possible to be. The Dursleys shuddered to think what the neighbors would
    say if the Potters arrived in the street. The Dursleys knew that the
    Potters had a small son, too, but they had never even seen him. This boy
    was another good reason for keeping the Potters away; they didn't want
    Dudley mixing with a child like that.[/lang='eng']
    [lang='ita']
    CAPITOLO UNO - 03<br>
    IL RAGAZZO SOPRAVVISSUTO<br>
    Mr e Mrs Dursley, di Privet Drive numero 4, erano orgogliosi di poter affermare che erano
    perfettamente normali e grazie tante. Erano le ultime persone al mondo da cui aspettarsi che
    avessero a che fare con cose strane o misteriose, perché sciocchezze del genere proprio non le approvavano.<br>
    Mr Dursley era direttore di una ditta di nome Grunnings, che fabbricava trapani. Era un uomo
    corpulento, nerboruto, quasi senza collo e con un grosso paio di baffi. Mrs Dursley era magra,
    bionda e con un collo quasi due volte più lungo del normale, il che le tornava assai utile, dato che
    passava gran parte del tempo ad allungarlo oltre la siepe del giardino per spiare i vicini.
    I Dursley avevano un figlioletto di nome Dudley e secondo loro non esisteva al mondo un bambino più bello.[/lang='ita']
</div>
<br><br><br><br>
<div>
    [lang='eng']
    CHAPTER ONE - 04<br>
    THE BOY WHO LIVED<br>
    Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say
    that they were perfectly normal, thank you very much. They were the last
    people you'd expect to be involved in anything strange or mysterious,
    because they just didn't hold with such nonsense.<br>
    Mr. Dursley was the director of a firm called Grunnings, which made
    drills. He was a big, beefy man with hardly any neck, although he did
    have a very large mustache. Mrs. Dursley was thin and blonde and had
    nearly twice the usual amount of neck, which came in very useful as she
    spent so much of her time craning over garden fences, spying on the
    neighbors. The Dursleys had a small son called Dudley and in their
    opinion there was no finer boy anywhere.<br>
    The Dursleys had everything they wanted, but they also had a secret, and
    their greatest fear was that somebody would discover it. They didn't
    think they could bear it if anyone found out about the Potters. Mrs.
    Potter was Mrs. Dursley's sister, but they hadn't met for several years;
    in fact, Mrs. Dursley pretended she didn't have a sister, because her
    sister and her good-for-nothing husband were as unDursleyish as it was
    possible to be. The Dursleys shuddered to think what the neighbors would
    say if the Potters arrived in the street. The Dursleys knew that the
    Potters had a small son, too, but they had never even seen him. This boy
    was another good reason for keeping the Potters away; they didn't want
    Dudley mixing with a child like that.[/lang='eng']
    [lang='ita']
    CAPITOLO UNO - 04<br>
    IL RAGAZZO SOPRAVVISSUTO<br>
    Mr e Mrs Dursley, di Privet Drive numero 4, erano orgogliosi di poter affermare che erano
    perfettamente normali e grazie tante. Erano le ultime persone al mondo da cui aspettarsi che
    avessero a che fare con cose strane o misteriose, perché sciocchezze del genere proprio non le approvavano.<br>
    Mr Dursley era direttore di una ditta di nome Grunnings, che fabbricava trapani. Era un uomo
    corpulento, nerboruto, quasi senza collo e con un grosso paio di baffi. Mrs Dursley era magra,
    bionda e con un collo quasi due volte più lungo del normale, il che le tornava assai utile, dato che
    passava gran parte del tempo ad allungarlo oltre la siepe del giardino per spiare i vicini.
    I Dursley avevano un figlioletto di nome Dudley e secondo loro non esisteva al mondo un bambino più bello.[/lang='ita']
</div>
<br><br><br><br>
<div>
    [lang='eng']
    CHAPTER ONE - 05<br>
    THE BOY WHO LIVED<br>
    Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say
    that they were perfectly normal, thank you very much. They were the last
    people you'd expect to be involved in anything strange or mysterious,
    because they just didn't hold with such nonsense.<br>
    Mr. Dursley was the director of a firm called Grunnings, which made
    drills. He was a big, beefy man with hardly any neck, although he did
    have a very large mustache. Mrs. Dursley was thin and blonde and had
    nearly twice the usual amount of neck, which came in very useful as she
    spent so much of her time craning over garden fences, spying on the
    neighbors. The Dursleys had a small son called Dudley and in their
    opinion there was no finer boy anywhere.<br>
    The Dursleys had everything they wanted, but they also had a secret, and
    their greatest fear was that somebody would discover it. They didn't
    think they could bear it if anyone found out about the Potters. Mrs.
    Potter was Mrs. Dursley's sister, but they hadn't met for several years;
    in fact, Mrs. Dursley pretended she didn't have a sister, because her
    sister and her good-for-nothing husband were as unDursleyish as it was
    possible to be. The Dursleys shuddered to think what the neighbors would
    say if the Potters arrived in the street. The Dursleys knew that the
    Potters had a small son, too, but they had never even seen him. This boy
    was another good reason for keeping the Potters away; they didn't want
    Dudley mixing with a child like that.[/lang='eng']
    [lang='ita']
    CAPITOLO UNO - 05<br>
    IL RAGAZZO SOPRAVVISSUTO<br>
    Mr e Mrs Dursley, di Privet Drive numero 4, erano orgogliosi di poter affermare che erano
    perfettamente normali e grazie tante. Erano le ultime persone al mondo da cui aspettarsi che
    avessero a che fare con cose strane o misteriose, perché sciocchezze del genere proprio non le approvavano.<br>
    Mr Dursley era direttore di una ditta di nome Grunnings, che fabbricava trapani. Era un uomo
    corpulento, nerboruto, quasi senza collo e con un grosso paio di baffi. Mrs Dursley era magra,
    bionda e con un collo quasi due volte più lungo del normale, il che le tornava assai utile, dato che
    passava gran parte del tempo ad allungarlo oltre la siepe del giardino per spiare i vicini.
    I Dursley avevano un figlioletto di nome Dudley e secondo loro non esisteva al mondo un bambino più bello.[/lang='ita']
</div>
<br><br><br><br>
<div>
CHAPTER ONE - 06<br>
THE BOY WHO LIVED<br>
Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say
that they were perfectly normal, thank you very much. They were the last
people you'd expect to be involved in anything strange or mysterious,
because they just didn't hold with such nonsense.<br>
Mr. Dursley was the director of a firm called Grunnings, which made
drills. He was a big, beefy man with hardly any neck, although he did
have a very large mustache. Mrs. Dursley was thin and blonde and had
nearly twice the usual amount of neck, which came in very useful as she
spent so much of her time craning over garden fences, spying on the
neighbors. The Dursleys had a small son called Dudley and in their
opinion there was no finer boy anywhere.<br>
The Dursleys had everything they wanted, but they also had a secret, and
their greatest fear was that somebody would discover it. They didn't
think they could bear it if anyone found out about the Potters. Mrs.
Potter was Mrs. Dursley's sister, but they hadn't met for several years;
in fact, Mrs. Dursley pretended she didn't have a sister, because her
sister and her good-for-nothing husband were as unDursleyish as it was
possible to be. The Dursleys shuddered to think what the neighbors would
say if the Potters arrived in the street. The Dursleys knew that the
Potters had a small son, too, but they had never even seen him. This boy
was another good reason for keeping the Potters away; they didn't want
Dudley mixing with a child like that.<br>
CAPITOLO UNO - 06<br>
IL RAGAZZO SOPRAVVISSUTO<br>
Mr e Mrs Dursley, di Privet Drive numero 4, erano orgogliosi di poter affermare che erano
perfettamente normali e grazie tante. Erano le ultime persone al mondo da cui aspettarsi che
avessero a che fare con cose strane o misteriose, perché sciocchezze del genere proprio non le approvavano.<br>
Mr Dursley era direttore di una ditta di nome Grunnings, che fabbricava trapani. Era un uomo
corpulento, nerboruto, quasi senza collo e con un grosso paio di baffi. Mrs Dursley era magra,
bionda e con un collo quasi due volte più lungo del normale, il che le tornava assai utile, dato che
passava gran parte del tempo ad allungarlo oltre la siepe del giardino per spiare i vicini.
I Dursley avevano un figlioletto di nome Dudley e secondo loro non esisteva al mondo un bambino più bello.
</div>

[lang='eng']
第一章-01
生活的男孩
女贞路四号的杜斯利夫妇自豪地说 他们都很正常,非常感谢。他们是最后一个 那些你希望参与任何奇怪或神秘的事情的人, 因为他们就是不相信这种胡说八道。
杜斯利先生是一家名为格鲁宁斯的公司的董事,该公司 训练。他身材魁梧,几乎没有脖子,尽管他有 我的胡子很大。德思礼太太身材瘦削,金发碧眼,有一双漂亮的眼睛 几乎是平时脖子的两倍,这对她来说非常有用 她花了那么多时间在花园的篱笆上,监视着花园里的树木 邻居。德思礼一家有一个小儿子,名叫达德利,在他们家里 没有比这更好的男孩了。
德思礼一家拥有他们想要的一切,但他们也有一个秘密,而且 他们最大的恐惧是有人会发现它。他们没有 我想如果有人发现波特一家,他们会忍受的。夫人 波特是德思礼太太的妹妹,但他们已经好几年没见面了; 事实上,德思礼太太假装她没有妹妹,因为她有一个妹妹 姐姐和她那一无是处的丈夫都是如此的放荡不羁 可能是。德思礼一家想到邻居们会怎么想就不寒而栗 如果波特一家来到街上。德思礼一家知道 波特夫妇也有一个小儿子,但他们从未见过他。这个男孩 这是另一个让陶工远离的好理由;他们不想要 达力和那样的孩子混在一起。[/lang='eng'] [lang='ita'] 卡皮托洛乌诺-01
拉加佐·索普拉维苏托酒店 德思礼先生和夫人,女贞路4号,埃拉诺·奥格格利奥西·波特·阿弗马尔·切·埃拉诺 正常的表演。埃拉诺·勒乌尔蒂姆·埃拉诺·勒乌尔蒂姆·埃拉诺·勒乌尔蒂姆·埃拉诺·勒乌尔蒂姆·阿尔·蒙多·达·崔·斯佩塔西 如果您有错误,请自行决定是否批准。
杜斯利先生时代的迪雷托·迪塔·迪塔·诺姆·格朗宁先生,切·法布里卡瓦·特拉帕尼。乌莫时代 corpulento、nerboruto和巴菲的grosso paio酒店。德思礼夫人, 在阿萨乌提尔省的阿萨乌提尔省的阿萨乌提尔省的阿萨乌提尔省的一个城市,一个准正常的城市 帕萨瓦·格兰德·帕萨瓦·帕萨瓦·格兰德·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·格兰德·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·帕萨瓦·帕。 我是杜斯利·阿维瓦诺·诺姆·达德利和第二个非埃西斯特瓦·蒙多·巴比诺·皮贝罗。[/lang='ita'] [lang='eng'] 第一章-01b
生活的男孩
女贞路四号的杜斯利夫妇自豪地说 他们都很正常,非常感谢。他们是最后一个 那些你希望参与任何奇怪或神秘的事情的人, 因为他们就是不相信这种胡说八道。
杜斯利先生是一家名为格鲁宁斯的公司的董事,该公司 训练。他身材魁梧,几乎没有脖子,尽管他有 我的胡子很大。德思礼太太身材瘦削,金发碧眼,有一双漂亮的眼睛 几乎是平时脖子的两倍,这对她来说非常有用 她花了那么多时间在花园的篱笆上,监视着花园里的树木 邻居。德思礼一家有一个小儿子,名叫达德利,在他们家里 没有比这更好的男孩了。
德思礼一家拥有他们想要的一切,但他们也有一个秘密,而且 他们最大的恐惧是有人会发现它。他们没有 我想如果有人发现波特一家,他们会忍受的。夫人 波特是德思礼太太的妹妹,但他们已经好几年没见面了; 事实上,德思礼太太假装她没有妹妹,因为她有一个妹妹 姐姐和她那一无是处的丈夫都是如此的放荡不羁 可能是。德思礼一家想到邻居们会怎么想就不寒而栗 如果波特一家来到街上。德思礼一家知道 波特夫妇也有一个小儿子,但他们从未见过他。这个男孩 这是另一个让陶工远离的好理由;他们不想要 达力和那样的孩子混在一起。[/lang='eng'] [lang='ita'] CAPITOLO UNO-01b
拉加佐·索普拉维苏托酒店 德思礼先生和夫人,女贞路4号,埃拉诺·奥格格利奥西·波特·阿弗马尔·切·埃拉诺 正常的表演。埃拉诺·勒乌尔蒂姆·埃拉诺·勒乌尔蒂姆·埃拉诺·勒乌尔蒂姆·埃拉诺·勒乌尔蒂姆·阿尔·蒙多·达·崔·斯佩塔西 如果您有错误,请自行决定是否批准。
杜先生
$("div").each(function(){
    var $this = $(this);   //<-----cache here
    texts.push($this.html()); // <----change here
    textToEdit =  texts[i];
    startPosEng = textToEdit.search(pattEng1) + 12;
    endPosEng = textToEdit.search(pattEng2) - startPosEng;
    textToEdit = textToEdit.replace(textToEdit.substr(startPosEng,endPosEng),"");
    try {
        $this.html(textToEdit); /// <-----here
    }
    catch(err) {
       $("#demo").html(err.message);
    }
    j = 0;
    while(j < langs.length) {
        textToEdit = $this.html(); // <----here
        textToEdit = textToEdit.replace("[lang='"+langs[j]+"']", "");
        textToEdit = textToEdit.replace("[\/lang='"+langs[j]+"']", "")
        $this.html(textToEdit); ///<------and here
        j++;
    }
    i++;
});