回答編集履歴
5
ねんのため
answer
CHANGED
@@ -96,4 +96,45 @@
|
|
96
96
|
<li id="test">テスト</li>
|
97
97
|
</ul>
|
98
98
|
</div>
|
99
|
+
```
|
100
|
+
|
101
|
+
# ローレベル処理
|
102
|
+
念の為もっとローレベルな処理を書いておきます
|
103
|
+
これならIEの相当古いバージョンでも動作します
|
104
|
+
|
105
|
+
```javascript
|
106
|
+
<script>
|
107
|
+
window.onload= function(){
|
108
|
+
var obj=document.getElementById('top-nav').getElementsByTagName('li');
|
109
|
+
for(var i=0;i<obj.length;i++){
|
110
|
+
var x=obj[i];
|
111
|
+
var links ={
|
112
|
+
"contact_form":"contact.html",
|
113
|
+
"privacypolicy":"privacypolicy.html"
|
114
|
+
};
|
115
|
+
var id=x.getAttribute("id");
|
116
|
+
var str="";
|
117
|
+
for(var j in links){
|
118
|
+
if(str!=="") str+="|";
|
119
|
+
str+=j;
|
120
|
+
}
|
121
|
+
var reg=new RegExp("^"+str+"$");
|
122
|
+
if(id.match(reg)){
|
123
|
+
var link1 = x.innerHTML;
|
124
|
+
var aTag = document.createElement("a");
|
125
|
+
aTag.setAttribute("href",links[id]);
|
126
|
+
aTag.appendChild(document.createTextNode(link1));
|
127
|
+
x.innerHTML="";
|
128
|
+
x.appendChild(aTag);
|
129
|
+
}
|
130
|
+
};
|
131
|
+
};
|
132
|
+
</script>
|
133
|
+
<div id="top-nav">
|
134
|
+
<ul id="fixed-nav">
|
135
|
+
<li id="contact_form">お問い合わせはコチラ</li>
|
136
|
+
<li id="privacypolicy">プライバシーポリシー</li>
|
137
|
+
<li id="test">テスト</li>
|
138
|
+
</ul>
|
139
|
+
</div>
|
99
140
|
```
|
4
修正
answer
CHANGED
@@ -62,4 +62,38 @@
|
|
62
62
|
<li id="contact_form100">お問い合わせ100</li>
|
63
63
|
</ul>
|
64
64
|
</div>
|
65
|
+
```
|
66
|
+
|
67
|
+
# 法則性がない
|
68
|
+
|
69
|
+
法則性がないなら、リストを羅列するしか無いですね
|
70
|
+
|
71
|
+
```javascript
|
72
|
+
<script>
|
73
|
+
window.addEventListener('DOMContentLoaded', function(e){
|
74
|
+
[].forEach.call(document.querySelectorAll('#top-nav li'),function(x){
|
75
|
+
var links ={
|
76
|
+
"contact_form":"contact.html",
|
77
|
+
"privacypolicy":"privacypolicy.html",
|
78
|
+
};
|
79
|
+
var id=x.getAttribute("id");
|
80
|
+
var reg=new RegExp("^"+Object.keys(links).join("|")+"$");
|
81
|
+
if(id.match(reg)){
|
82
|
+
var link1 = x.textContent;
|
83
|
+
var aTag = document.createElement("a");
|
84
|
+
aTag.setAttribute("href",links[id]);
|
85
|
+
aTag.appendChild(document.createTextNode(link1));
|
86
|
+
x.textContent="";
|
87
|
+
x.appendChild(aTag);
|
88
|
+
}
|
89
|
+
});
|
90
|
+
});
|
91
|
+
</script>
|
92
|
+
<div id="top-nav">
|
93
|
+
<ul id="fixed-nav">
|
94
|
+
<li id="contact_form">お問い合わせはコチラ</li>
|
95
|
+
<li id="privacypolicy">プライバシーポリシー</li>
|
96
|
+
<li id="test">テスト</li>
|
97
|
+
</ul>
|
98
|
+
</div>
|
65
99
|
```
|
3
typo
answer
CHANGED
@@ -39,11 +39,11 @@
|
|
39
39
|
window.addEventListener('DOMContentLoaded', function(e){
|
40
40
|
[].forEach.call(document.querySelectorAll('#top-nav li'),function(x){
|
41
41
|
var linkBasename = "contact";
|
42
|
-
var
|
42
|
+
var linkExtension = ".html";
|
43
43
|
var link1 = x.textContent;
|
44
44
|
if(r=x.getAttribute('id').match(/^contact_form(.+)$/)){
|
45
45
|
var aTag = document.createElement("a");
|
46
|
-
aTag.setAttribute("href",linkBasename+r[1]+
|
46
|
+
aTag.setAttribute("href",linkBasename+r[1]+linkExtension)
|
47
47
|
aTag.appendChild(document.createTextNode(link1));
|
48
48
|
x.textContent="";
|
49
49
|
x.appendChild(aTag);
|
2
修正
answer
CHANGED
@@ -30,4 +30,36 @@
|
|
30
30
|
<li id="contact_form6">お問い合わせ6</li>
|
31
31
|
</ul>
|
32
32
|
</div>
|
33
|
+
```
|
34
|
+
|
35
|
+
# 修正
|
36
|
+
idのcontact_form以降の数値を利用してconctactxxx.htmlにリンクを張ればいいのですね?
|
37
|
+
```javascript
|
38
|
+
<script>
|
39
|
+
window.addEventListener('DOMContentLoaded', function(e){
|
40
|
+
[].forEach.call(document.querySelectorAll('#top-nav li'),function(x){
|
41
|
+
var linkBasename = "contact";
|
42
|
+
var linkExtendname = ".html";
|
43
|
+
var link1 = x.textContent;
|
44
|
+
if(r=x.getAttribute('id').match(/^contact_form(.+)$/)){
|
45
|
+
var aTag = document.createElement("a");
|
46
|
+
aTag.setAttribute("href",linkBasename+r[1]+linkExtendname)
|
47
|
+
aTag.appendChild(document.createTextNode(link1));
|
48
|
+
x.textContent="";
|
49
|
+
x.appendChild(aTag);
|
50
|
+
};
|
51
|
+
});
|
52
|
+
});
|
53
|
+
</script>
|
54
|
+
<div id="top-nav">
|
55
|
+
<ul>
|
56
|
+
<li id="contact_form1">お問い合わせ1</li>
|
57
|
+
<li id="contact_form2">お問い合わせ2</li>
|
58
|
+
<li id="contact_form3">お問い合わせ3</li>
|
59
|
+
<li id="contact_form4">お問い合わせ4</li>
|
60
|
+
<li id="contact_form5">お問い合わせ5</li>
|
61
|
+
<li id="contact_form6">お問い合わせ6</li>
|
62
|
+
<li id="contact_form100">お問い合わせ100</li>
|
63
|
+
</ul>
|
64
|
+
</div>
|
33
65
|
```
|
1
追記
answer
CHANGED
@@ -2,4 +2,32 @@
|
|
2
2
|
- idがcontact_formxxx(xxxは数値)のものを拾う
|
3
3
|
- 「contact_form」classを付けてそれを拾う
|
4
4
|
|
5
|
-
のどれかになるでしょう
|
5
|
+
のどれかになるでしょう
|
6
|
+
# 追記
|
7
|
+
とりあえず,top-navのliをひろってみます
|
8
|
+
|
9
|
+
```javascript
|
10
|
+
<script>
|
11
|
+
window.addEventListener('DOMContentLoaded', function(e){
|
12
|
+
[].forEach.call(document.querySelectorAll('#top-nav li'),function(x){
|
13
|
+
var linkURL = "contact.html";
|
14
|
+
var link1 = x.textContent;
|
15
|
+
var aTag = document.createElement("a");
|
16
|
+
aTag.setAttribute("href",linkURL)
|
17
|
+
aTag.appendChild(document.createTextNode(link1));
|
18
|
+
x.textContent="";
|
19
|
+
x.appendChild(aTag);
|
20
|
+
});
|
21
|
+
});
|
22
|
+
</script>
|
23
|
+
<div id="top-nav">
|
24
|
+
<ul>
|
25
|
+
<li id="contact_form1">お問い合わせ1</li>
|
26
|
+
<li id="contact_form2">お問い合わせ2</li>
|
27
|
+
<li id="contact_form3">お問い合わせ3</li>
|
28
|
+
<li id="contact_form4">お問い合わせ4</li>
|
29
|
+
<li id="contact_form5">お問い合わせ5</li>
|
30
|
+
<li id="contact_form6">お問い合わせ6</li>
|
31
|
+
</ul>
|
32
|
+
</div>
|
33
|
+
```
|