回答編集履歴
2
バグがあったので修正・説明の追加
answer
CHANGED
@@ -1,21 +1,29 @@
|
|
1
|
+
class="aiueo"が複数あっても動作します。
|
2
|
+
|
1
3
|
```javascript
|
4
|
+
/*jshint browser: true, jquery: true*/
|
5
|
+
|
2
6
|
$(function() {
|
3
|
-
var aiueo = $('.aiueo'); // 2回使うので変数に入れ
|
7
|
+
var aiueo = $('.aiueo'); // 2回使うので変数に入れて処理削減
|
4
|
-
var
|
8
|
+
var aiueoHtmlArray, aiueoThis;
|
9
|
+
|
5
10
|
aiueo.each(function() { // class="aiueo"が複数あるかもしれないのですべてに対して処理
|
11
|
+
|
6
|
-
|
12
|
+
aiueoThis = $(this); // 3回使うので変数に入れて処理削減(thisはここではn個目のclass="aiueo"を表します)
|
7
|
-
|
13
|
+
aiueoHtmlArray = aiueoThis.html().split('<hr>'); // thisのHTMLを取得し、hr要素で分割して配列に代入
|
8
|
-
|
14
|
+
aiueoThis.empty(); // thisの内容を削除
|
9
|
-
$(
|
15
|
+
$(aiueoHtmlArray).each(function(index) { // aiueoHtmlArrayの各要素に対して処理
|
10
|
-
|
16
|
+
aiueoThis.append(function() { // thisの最後に追加
|
11
|
-
if(
|
17
|
+
if(aiueoHtmlArray[index].indexOf('ざーざー') != -1) { // 「ざーざー」という文字列が含まれたら
|
12
|
-
return '<span style="background-color: red;">' +
|
18
|
+
return '<span style="background-color: red;">' + aiueoHtmlArray[index] + '</span><hr>';
|
13
19
|
} else { // 「ざーざー」という文字列が含まれていなかったら
|
14
|
-
return
|
20
|
+
return aiueoHtmlArray[index] + '<hr>';
|
15
21
|
}
|
16
22
|
});
|
17
23
|
});
|
18
|
-
|
24
|
+
|
19
25
|
});
|
20
26
|
});
|
21
|
-
```
|
27
|
+
```
|
28
|
+
|
29
|
+
(10/31 07:52 一部修正しました)
|
1
コメントの追加・修正
answer
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
$(function() {
|
3
3
|
var aiueo = $('.aiueo'); // 2回使うので変数に入れる
|
4
4
|
var aiueoHtml, aiueoArray;
|
5
|
-
aiueo.each(function() {
|
5
|
+
aiueo.each(function() { // class="aiueo"が複数あるかもしれないのですべてに対して処理
|
6
6
|
aiueoHtml = $(this).html(); // htmlを取得
|
7
7
|
aiueoArray = aiueoHtml.split('<hr>'); // hr要素で分割して配列に代入
|
8
|
-
aiueo.empty(); //
|
8
|
+
aiueo.empty(); // class="aiueo"の内容を削除
|
9
9
|
$(aiueoArray).each(function(index) { // 配列の各要素に対して処理
|
10
|
-
aiueo.append(function() { // 最後に追加
|
10
|
+
aiueo.append(function() { // class="aiueo"の最後に追加
|
11
|
-
if(aiueoArray[index].indexOf('ざーざー') != -1) { //「ざーざー」という文字列が含まれたら
|
11
|
+
if(aiueoArray[index].indexOf('ざーざー') != -1) { // 「ざーざー」という文字列が含まれたら
|
12
12
|
return '<span style="background-color: red;">' + aiueoArray[index] + '</span><hr>';
|
13
|
-
} else {
|
13
|
+
} else { // 「ざーざー」という文字列が含まれていなかったら
|
14
14
|
return aiueoArray[index] + '<hr>';
|
15
15
|
}
|
16
16
|
});
|