-
Notifications
You must be signed in to change notification settings - Fork 35
/
properties.html
202 lines (185 loc) · 8.34 KB
/
properties.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Работа с атрибутами и свойствами элементов</title>
<link rel="profile" href="https://gmpg.org/xfn/11"/>
<link rel="shortcut icon" href="https://anton.shevchuk.name/favicon.ico"/>
<link rel="stylesheet" href="css/styles.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/code.js"></script>
<script>
$(function(){
$('#target').submit(function () {
output($(this).serialize() + '\n');
return false;
});
let $checkbox1 = $('#checkbox');
let $checkbox1AttrChecked = $('#checkbox1-attr-checked');
let $checkbox1PropChecked = $('#checkbox1-prop-checked');
let $checkbox1AttrDisabled = $('#checkbox1-attr-disabled');
let $checkbox1PropDisabled = $('#checkbox1-prop-disabled');
let $checkbox2 = $('#checkbox-two');
let $checkbox2AttrChecked = $('#checkbox2-attr-checked');
let $checkbox2PropChecked = $('#checkbox2-prop-checked');
let $radio1 = $('#radio-one');
let $radio1AttrChecked = $('#radio1-attr-checked');
let $radio1PropChecked = $('#radio1-prop-checked');
let $radio2 = $('#radio-two');
let $radio2AttrDisabled = $('#radio2-attr-disabled');
let $radio2PropDisabled = $('#radio2-prop-disabled');
let $radio3 = $('#radio-three');
let $radio3AttrChecked = $('#radio3-attr-checked');
let $radio3PropChecked = $('#radio3-prop-checked');
setInterval(function () {
$checkbox1AttrChecked.text($checkbox1.attr('checked') ? 'checked' : 'false');
$checkbox1PropChecked.text($checkbox1.prop('checked'));
$checkbox1AttrDisabled.text($checkbox1.attr('disabled') ? 'disabled' : 'false');
$checkbox1PropDisabled.text($checkbox1.prop('disabled'));
$checkbox2AttrChecked.text($checkbox2.attr('checked') ? 'checked' : 'false');
$checkbox2PropChecked.text($checkbox2.prop('checked'));
$radio1AttrChecked.text($radio1.attr('checked') ? 'checked' : 'false');
$radio1PropChecked.text($radio1.prop('checked'));
$radio2AttrDisabled.text($radio2.attr('disabled') ? 'disabled' : 'false');
$radio2PropDisabled.text($radio2.prop('disabled'));
$radio3AttrChecked.text($radio3.attr('checked') ? 'checked' : 'false');
$radio3PropChecked.text($radio3.prop('checked'));
}, 300)
});
</script>
</head>
<body>
<header>
<h1>Пример работы с атрибутами и свойствами элементов</h1>
<h2>Почувствуй разницу между <code>attr()</code> и <code>prop()</code></h2>
</header>
<main>
<article>
<h3>Форма проб и ошибок</h3>
<form id="target" action="">
<fieldset>
<legend>Чекбоксы</legend>
<table class="w-full">
<tr>
<td class="w-1/4">
<input id="checkbox" type="checkbox" name="checkbox-one" value="1"/>
<label for="checkbox">чекбокс</label>
</td>
<td class="w-3/4">
<code class="w-full p-2">
attr('checked') == <span id="checkbox1-attr-checked">?</span><br/>
prop('checked') == <span id="checkbox1-prop-checked">?</span><br/>
attr('disabled') == <span id="checkbox1-attr-disabled">?</span><br/>
prop('disabled') == <span id="checkbox1-prop-disabled">?</span>
</code>
</td>
</tr>
<tr>
<td>
<input id="checkbox-two" type="checkbox" name="checkbox-two" value="1" checked="checked"/>
<label for="checkbox-two">ещё один</label>
</td>
<td>
<code class="w-full p-2">
attr('checked') == <span id="checkbox2-attr-checked">?</span><br/>
prop('checked') == <span id="checkbox2-prop-checked">?</span>
</code>
</td>
</tr>
</table>
</fieldset>
<br/>
<fieldset>
<legend>Радиобаттоны</legend>
<table class="w-full">
<tr>
<td class="w-1/4">
<input id="radio-one" type="radio" name="radio" value="1" checked/>
<label for="radio-one">радио один</label>
</td>
<td class="w-3/4">
<code class="w-full p-2">
attr('checked') == <span id="radio1-attr-checked">?</span><br/>
prop('checked') == <span id="radio1-prop-checked">?</span><br/>
</code>
</td>
</tr>
<tr>
<td>
<input id="radio-two" type="radio" name="radio" value="2" disabled/>
<label for="radio-two">второе радио</label>
</td>
<td>
<code class="w-full p-2">
attr('disabled') == <span id="radio2-attr-disabled">?</span><br/>
prop('disabled') == <span id="radio2-prop-disabled">?</span>
</code>
</td>
</tr>
<tr>
<td>
<input id="radio-three" type="radio" name="radio" value="3"/>
<label for="radio-three">третье радио</label>
</td>
<td>
<code class="w-full p-2">
attr('checked') == <span id="radio3-attr-checked">?</span><br/>
prop('checked') == <span id="radio3-prop-checked">?</span><br/>
</code>
</td>
</tr>
</table>
</fieldset>
<button type="submit">Как оно там?</button>
</form>
</article>
</main>
<footer>
© <a href="https://anton.shevchuk.name/jquery-book/">jQuery for beginners</a>
</footer>
<aside>
<nav>
<a href="class.html" title="go prev" rel="prev">Prev</a>
<a href="index.html#20" title="back to Index" rel="index">Index</a>
<a href="#" title="reload" onclick="window.location.reload();return false">Reload</a>
<a href="height.html" title="go next" rel="next">Next</a>
</nav>
<hr/>
<div>
<p>Изменяем атрибуты</p>
<code><em>// "check"</em>
$(<span>'#checkbox'</span>)
.attr(<span>'checked'</span>, <i>true</i>)</code>
<button type="button">Run Code</button>
<code><em>// "uncheck"</em>
$(<span>'#checkbox'</span>)
.attr(<span>'checked'</span>, <i>false</i>)</code>
<button type="button">Run Code</button>
<hr/>
<p>Изменяем свойства</p>
<code><em>// check</em>
$(<span>'#checkbox'</span>)
.prop(<span>'checked'</span>, <i>true</i>)</code>
<button type="button">Run Code</button>
<code><em>// uncheck</em>
$(<span>'#checkbox'</span>)
.prop(<span>'checked'</span>, <i>false</i>)</code>
<button type="button">Run Code</button>
<code><em>// disable checkbox</em>
$(<span>'#checkbox'</span>)
.prop(<span>'disabled'</span>, <i>true</i>)</code>
<button type="button">Run Code</button>
<code><em>// enable checkbox</em>
$(<span>'#checkbox'</span>)
.prop(<span>'disabled'</span>, <i>false</i>)</code>
<button type="button">Run Code</button>
</div>
</aside>
<aside id="output">
<h4>Состояние формы:</h4>
<hr/>
<code></code>
</aside>
</body>
</html>