# jQuery one() Method (ok)

<https://hocwebchuan.com/reference/jquery/jquery_one.php>

* .one(): Chèn một hoặc nhiều sự kiện cho một thành phần được chọn, xác định một function để chạy khi xảy ra một sự kiện.

* Khi sử dụng .one(), function xử lý sự kiện chỉ chạy một lần duy nhất cho mỗi thành phần.

* Đã được thêm vào từ phiên bản 1.1

[**.one('Sự kiện',function(){...})**](https://hocwebchuan.com/reference/jquery/jquery_one.php#anchor01)

```
$('.test').one('click',function(){
    alert('Sự kiện này chỉ hiển thị một lần.');
});
```

* Đã được thêm vào từ phiên bản 1.7

[**.one('Sự kiện','bộ chọn',function(){...})**](https://hocwebchuan.com/reference/jquery/jquery_one.php#anchor02)

```
$('body').one('click','.test',function(){
    alert('Sự kiện này chỉ hiển thị một lần.');
});
```

#### .one('Sự kiện',function(){...})

**Html viết:**

```
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tiêu đề</title>
<script src="https://code.jquery.com/jquery-latest.js"></script>
<style>
p {
    background: blue;
    border: 10px outset;
    float: left;
    height: 50px;
    margin: 5px;
    width: 50px;
}
</style>
<script>
$(function(){
    $('p').one('click',function(){
         $(this).css({borderStyle: "inset"});
    });
});
</script>
</head>

<body>
<p>P</p>
<p>P</p>
<p>P</p>
<p>P</p>
</body>
</html>
```

**Hiển thị trình duyệt:**

Click vào từng thành phần trên để thấy kết quả, sự kiện chỉ thực hiện được một lần cho mỗi thành phần duy nhất.

**So sánh code HTML trước và sau khi có jQuery:**

| Trước khi có jQuery                                         | Sau khi có jQuery                                                                                 |
| ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| <p>\<p>P\</p><br>\<p>P\</p><br>\<p>P\</p><br>\<p>P\</p></p> | <p>\<p>P\</p><br>\<p <em>style="border-style: inset;"</em>>P\</p><br>\<p>P\</p><br>\<p>P\</p></p> |

#### .one('Sự kiện','bộ chọn',function(){...})

**Html viết:**

```
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tiêu đề</title>
<script src="https://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
    $('body').one('click','p',function(){
         $(this).animate({fontSize:"+=1em"});
    });
});
</script>
</head>

<body>
<p>Thành phần P</p>
<div>Thành phần DIV</div>
</body>
</html>
```

**Hiển thị trình duyệt:**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/advanced/jquery-one-method-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
