var text = "Andorras tourism services an estimated 10.2 million visitors annually.[1]
It has been a member of the United Nations since 1993.[2]
In the world at 81 years, according to The Lancet.[3]
[1]HOTELERIA I TURISME. Retrieved 14 May 2015.
[2]United Nations Member States. Un.org. Retrieved 2015-05-14.
[2]United Nations Member States. Un.org. Retrieved 2015-05-14.
[2]United Nations Member States. Un.org. Retrieved 2015-05-14."
如何找到每个符号[ ]
的数字。如果检测到标记[ ]
内有数字,则相同的数字如此" annually.[1] ...... [1]HOTELERIA
&#34;。因此,请在第一个数字上添加<span class="bignote">
,在第二个数字上添加<span class="smallnote">
但如果[]
符号中只有一个数字。或者[]
符号中有两个以上相同的数字,它是不可检测的
像这样输出,只有[1]才能获得范围代码
Andorras tourism services an estimated 10.2 million visitors annually.<span class="bignote">[1]</span>
It has been a member of the United Nations since 1993.[2]
In the world at 81 years, according to The Lancet.[3]
<span class="smallnote">[1]</span>HOTELERIA I TURISME. Retrieved 14 May 2015.
[2]United Nations Member States. Un.org. Retrieved 2015-05-14.
[2]United Nations Member States. Un.org. Retrieved 2015-05-14.
[2]United Nations Member States. Un.org. Retrieved 2015-05-14.
更新:
[2]
不能是bignote和smallnote因为[2]
有两个以上,从文本内容来看,有四个符号[2]
[3]
不能成为bignote和smallnote因为[3]
只有一个
var text = "Andorras tourism services an estimated 10.2 million visitors annually.[1]<br>It has been a member of the United Nations since 1993.[2]<br>In the world at 81 years, according to The Lancet.[3]<br><br><br><br>Note: <br>[1]HOTELERIA I TURISME. Retrieved 14 May 2015. <br>[2]United Nations Member States. Un.org. Retrieved 2015-05-14. <br>[2]United Nations Member States. Un.org. Retrieved 2015-05-14. <br>[2]United Nations Member States. Un.org. Retrieved 2015-05-14."
$("#text").html(text);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="text">
</div>
&#13;
更新Louys Patrice Bessette
如果您看到此https://jsfiddle.net/bobha9gc/
如果我在 textarea内容中输入[1]
,那么 textarea note 会自动添加内容[1]
。但是如果我在 textarea内容中删除它。 textarea note 也将被删除
[1]
的内容中的{p> <sup id="1">[1]</sup>
为[1]
和<span id="1">[1]</span>
这不像我之前的问题那样限制。如果内容中有多个[1] [1]
个,则会将其插入到便笺中
最后,如何检测textarea笔记是否未被填充。
如果我没有填写部分[4]
,则无法发送或启动alert()
答案 0 :(得分:0)
我不确定您是否意识到问题的复杂性。
下次,当系统要求您提供HTML或任何其他有关代码的信息时,请将其发布 你并不总是落在像我这样的病人身上。
那说......我绝不会像你一样在JS变量中剪切和粘贴整个文本。我会在我的HTML中使用它,如下例所示,然后在相关部分上运行脚本。
这是一个&#34;编码最佳实践&#34;,如果我可以说...它避免了问题字符的问题,如双引号"
。
<小时/> 现在,脚本......
然后,您需要检查是否找到了数字:
如果它位于文本或中,则必须在其周围添加span
元素,如果它在注释中,则必须添加另一个类。
你同意它并不简单......
我确信您希望脚本能够处理许多不同的文本和注释块。
... []符号内的两个以上相同的数字,它是不可检测的。
我做的方式,它适用于具有多个数字的数字,而不是你的假设。
我使用了for
个循环,以及.match()
,.indexOf()
,.push()
,.replace()
......等方法
我将它们应用于分为两部分,主要文本和注释的文本。
我认为相同的数字在主要文本中可以出现多次。
请查看以下代码段或CodePen
代码评论很好。
var spans = [];
// The pattern to look for.
var pattern = /\[\d+\]/gm;
// The two HTML text chunks to work on.
var text = $(".text").html();
var notes = $(".notes").html();
// Two arrays of text matching the pattern.
var match_text = text.match(pattern);
var match_notes = notes.match(pattern);
// Some console logs.
console.log( JSON.stringify(match_text) );
console.log( JSON.stringify(match_notes) );
// Choose the longuest for the comparison loop.
var longer_array = match_text;
var shorter_array = match_notes;
if(match_text.length < match_notes){
text_longer = match_notes;
shorter_array = match_text;
console.log("Less matches in text.");
}
// Comparison loop.
for(i=0;i<longer_array.length;i++){
if( shorter_array.indexOf(longer_array[i]) != -1){
console.log(longer_array[i]+" FOUND IN BOTH ARRAYS.");
// An inner loop to check if the text found occurs more than once in notes.
var notes_occurance = 0;
for(j=0;j<match_notes.length;j++){
if( longer_array[i] == match_notes[j] ){
notes_occurance++;
}
}
// If there is only one occurance, keep that text in the spans array.
if(notes_occurance == 1){
spans.push(longer_array[i]);
}else{
console.log(longer_array[i]+" is in notes more than once.")
}
}
}
// Console log the result.
console.log( JSON.stringify(spans) );
// Now add spans to both text chunks.
for(i=0;i<spans.length;i++){
text = text.replace(spans[i], "<span class='bignote'>"+spans[i]+"</span>");
notes = notes.replace(spans[i], "<span class='smallnote'>"+spans[i]+"</span>");
// Update HTML.
$(".text").html(text);
$(".notes").html(notes);
}
&#13;
.bignote{
background-color: #37EF40; /* green */
}
.smallnote{
background-color: #EFE937; /* yellow */
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text">
Andorras tourism services an estimated 10.2 million visitors annually.[1]<br>
It has been a member of the United Nations since 1993.[2]<br>
In the world at 81 years, according to The Lancet.[3]<br>
<br>
<br>
<br>
</div>
<div class="notes">
Note: <br>
[1]HOTELERIA I TURISME. Retrieved 14 May 2015. <br>
[2]United Nations Member States. Un.org. Retrieved 2015-05-14. <br>
[2]United Nations Member States. Un.org. Retrieved 2015-05-14. <br>
[2]United Nations Member States. Un.org. Retrieved 2015-05-14.
</div>
&#13;