回答編集履歴
3
加筆
answer
CHANGED
@@ -12,13 +12,13 @@
|
|
12
12
|
今回は `this.href` を使いました。
|
13
13
|
|
14
14
|
- `$(this).attr("href")` は、**href属性の文字列をそのまま**取得します。
|
15
|
-
- `this.href` は実際にリンクする先の**フルのURL**を取得できます。
|
15
|
+
- `$(this).prop("href")`(あるいは単純に `this.href`) は実際にリンクする先の**フルのURL**を取得できます。
|
16
16
|
|
17
17
|
```html
|
18
18
|
<a id="link" href="hoge.html">リンク</a>
|
19
19
|
<script>
|
20
20
|
console.log($("#link").attr("href")); // hoge.html
|
21
|
-
console.log($("#link").
|
21
|
+
console.log($("#link").prop("href")); // http://○○○.○○○/○○○/hoge.html
|
22
22
|
</script>
|
23
23
|
```
|
24
24
|
|
2
コードの修正
answer
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
```javascript
|
4
4
|
$(function(){
|
5
|
-
$("a").each(function(){
|
5
|
+
$(".side__navigation a").each(function(){
|
6
6
|
if(this.href == location.href) $(this).addClass("is_active");
|
7
7
|
});
|
8
8
|
});
|
1
誤字の修正
answer
CHANGED
@@ -8,18 +8,18 @@
|
|
8
8
|
});
|
9
9
|
```
|
10
10
|
|
11
|
-
以前の質問
|
11
|
+
以前の質問を拝見しましたが、`$(this).attr("href")` を使っていますね。
|
12
12
|
今回は `this.href` を使いました。
|
13
13
|
|
14
|
-
- `$(this).attr("href")` は、href属性の文字列をそのまま取得します。
|
14
|
+
- `$(this).attr("href")` は、**href属性の文字列をそのまま**取得します。
|
15
|
-
- `this.href` は実際にリンクする先のフルのURLを取得できます。
|
15
|
+
- `this.href` は実際にリンクする先の**フルのURL**を取得できます。
|
16
16
|
|
17
|
-
ですので、`this.href` を利用すれば、`location.href` との単純比較で現在URLと一致しているかどうか確認することが出来ます。
|
18
|
-
|
19
17
|
```html
|
20
18
|
<a id="link" href="hoge.html">リンク</a>
|
21
19
|
<script>
|
22
20
|
console.log($("#link").attr("href")); // hoge.html
|
23
21
|
console.log($("#link").get(0).href); // http://○○○.○○○/○○○/hoge.html
|
24
22
|
</script>
|
25
|
-
```
|
23
|
+
```
|
24
|
+
|
25
|
+
ですので、`this.href` を利用すれば `location.href` との単純比較で現在URLと一致しているかどうか確認することが出来ます。
|