-
Notifications
You must be signed in to change notification settings - Fork 8k
/
index.md
84 lines (62 loc) · 3.26 KB
/
index.md
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
---
title: GlobalEventHandlers.onclick
slug: Web/API/Element/click_event
---
{{APIRef("HTML DOM")}}
Свойство **onclick** возвращает обработчик события `click` на текущем элементе.
> [!NOTE]
> При использовании обработчика события `click` для вызова любого действия, убедитесь, что событие `keydown` имеет такое же действие. Это нужно для того, чтобы пользователи, которые не используют мышь или тачскрин могли использовать то же действие.
## Синтаксис
```
element.>
```
где _functionRef_ это функция - зачастую это имя функции, которая объявлена где-то в другом месте или же _функциональное выражение_. См. "[JavaScript Guide:Functions](/ru/docs/JavaScript/Guide/Functions)".
Единственный аргумент, переданный в определённую функцию обработчик события это {{domxref("MouseEvent")}} объект. `this` внутри обработчика будет указывать на элемент, на котором было вызвано событие.
## Пример
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>onclick event example</title>
<script>
function initElement() {
var p = document.getElementById("foo");
// NOTE: showAlert(); или showAlert(param); так НЕ сработает.
// Нужно использовать ссылку на функцию, но не вызов функции.
p.>
}
function showAlert(event) {
alert("onclick Event detected!");
}
</script>
<style>
#foo {
border: solid blue 2px;
}
</style>
</head>
<body >
<span id="foo">My Event Element</span>
<p>click on the above element.</p>
</body>
</html>
```
Или же вы можете использовать анонимную функцию, как здесь:
```js
p. (event) {
alert("moot!");
};
```
## Примечания
Событие `click` возникает, когда пользователь кликает на элемент. Событие `click` возникнет после событий `mousedown` и `mouseup`.
С данным свойством только один обработчик события `click` может быть назначен объекту. Возможно, вместо данного метода, вам стоит обратить внимание на метод {{domxref("EventTarget.addEventListener()")}}, т.к. он более гибкий и является частью спецификации DOM Events.
## Спецификации
{{Specifications}}
## Совместимость с браузерами
{{Compat}}
## Смотрите также
- {{event("click")}} event
- Related event handlers
- {{domxref("GlobalEventHandlers.onauxclick")}}
- {{domxref("GlobalEventHandlers.ondblclick")}}
You can’t perform that action at this time.