Sự kiện onclick thường xảy ra khi người dùng nhấp vào một phần tử. Nó cho phép lập trình viên thực thi chức năng của JavaScript khi một phần tử được nhấp vào. Sự kiện này có thể được sử dụng để xác thực biểu mẫu, thông báo cảnh báo, v.v. Show
Using JavaScript, this event can be dynamically added to any element. It supports all HTML elements except , , , , , In HTML, we can use the onclick attribute and assign a JavaScript function to it. We can also use the JavaScript's addEventListener() method and pass a click event to it for greater flexibility. SyntaxNow, we see the syntax of using the onclick event in HTML and in javascript (without addEventListener() method or by using the addEventListener() method). In HTMLIn JavaScriptIn JavaScript by using the addEventListener() methodLet's see how to use onclick event by using some illustrations. Now, we will see the examples of using the onclick event in HTML, and in JavaScript. Example1 - Using onclick attribute in HTMLIn this example, we are using the HTML onclick attribute and assigning a JavaScript's function to it. When the user clicks the given button, the corresponding function will get executed, and an alert dialog box will be displayed on the screen. Test it NowOutput After clicking the given button, the output will be - Example2 - Using JavaScriptIn this example, we are using JavaScript's onclick event. Here we are using the onclick event with the paragraph element. When the user clicks on the paragraph element, the corresponding function will get executed, and the text of the paragraph gets changed. On clicking the element, the background color, size, border, and color of the text will also get change. Test it NowOutput After clicking the text Click me, the output will be - Example3 - Using addEventListener() methodIn this example, we are using JavaScript's addEventListener() method to attach a click event to the paragraph element. When the user clicks the paragraph element, the text of the paragraph gets changed. On clicking the paragraph, the background color and font-size of elements will also change. Test it NowOutput On clicking the text Click me, the output will be - Bạn có thể sử dụng onclick mà không cần JavaScript không?Hầu hết mọi người sẽ nói với bạn rằng điều đó là không thể, tuy nhiên điều đó đơn giản là không đúng. Cách duy nhất để gửi dữ liệu mà không cần JavaScript là sử dụng các nút gửi. Các nút gửi có thể được tạo kiểu tương tự như văn bản nhưng nó không lý tưởng. Chỉ cần ẩn các nút gửi và sử dụng các nhãn có thuộc tính for tương ứng
Chúng tôi có thể sử dụng onclick trong HTML không?Thuộc tính onclick là một phần của Thuộc tính sự kiện và có thể được sử dụng trên mọi phần tử HTML .
Tôi có thể sử dụng khi nhấp trong div không?Cách đặt trình nghe onClick trên phần tử div trong React. Đặt giá đỡ onClick trên div . Hàm bạn chuyển đến chỗ dựa sẽ được gọi mỗi khi nhấp vào div.
Những phần tử HTML nào có thể có onclick?Tất cả phần tử HTML có thể có thuộc tính onclick. Xem đặc tả HTML 5 để xác nhận. (Tất nhiên, một số thành phần không được hiển thị theo mặc định, do đó bạn sẽ phải thay đổi thuộc tính hiển thị của chúng bằng CSS trước khi có thể nhấp vào chúng để kích hoạt trình xử lý sự kiện). |