Javascript thay đổi đoạn văn bản onclick

Đây là một tệp html mẫu có nút gửi. Bây giờ hãy sửa đổi kiểu của đoạn văn bản thông qua mã javascript

Tệp HTML mẫu



JS DOM paragraph style

JavaScript Exercises - w3resource

Style

Lời khuyên. Nhấp vào nút, phông chữ, cỡ chữ và màu sắc của đoạn văn bản sẽ được thay đổi

Giải pháp mẫu. -

Mã HTML





JS DOM paragraph style
 

JavaScript Exercises - w3resource

Style

Mã JavaScript

function js_style[] 
{
//font styles added by JS:
 text.style.fontSize = "14pt";
 text.style.fontFamily = "Comic Sans MS";
 text.style.color = "green";
}

Đầu ra mẫu

, , , , ,
, , and . It means we cannot apply the title event on the given tags.

In HTML, we can use the title 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.

Syntax

Now, we see the syntax of using the title event in HTML and in javascript [without addEventListener[] method or by using the addEventListener[] method].

In HTML

In JavaScript

In JavaScript by using the addEventListener[] method

Let's see how to use title event by using some illustrations. Now, we will see the examples of using the title event in HTML, and in JavaScript.

Example1 - Using title attribute in HTML

In this example, we are using the HTML title 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 Now

Output

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 Now

Output

Test it Now

Output

On clicking the text Click me, the output will be -

Làm thế nào JavaScript có thể được sử dụng để sửa đổi nội dung của một đoạn văn?

Cách dễ nhất để sửa đổi nội dung của phần tử HTML là sử dụng thuộc tính innerHTML. .
The HTML document above contains a

element with id="p1".

Chúng tôi sử dụng HTML DOM để lấy phần tử có id="p1"
Một JavaScript thay đổi nội dung [ innerHTML ] của phần tử đó thành "Văn bản mới. "

Tôi có thể sử dụng cái gì thay vì title?

Các sự kiện được sử dụng mà không có tiền tố “bật” như sử dụng “click” thay vì “ title” hoặc “mousedown” thay vì “onmousedown”. listener[hàm xử lý ]. Nó có thể là một hàm JavaScript phản hồi sự kiện xảy ra.

Bạn có thể thêm title vào 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. Bạn có thể truy cập div dưới dạng sự kiện.

Chúng ta có thể thêm title vào nút không?

Thuộc tính title là thuộc tính sự kiện được tất cả các trình duyệt hỗ trợ. Nó xuất hiện khi người dùng nhấp vào một phần tử nút. Nếu bạn muốn tạo nút title, bạn cần thêm thuộc tính sự kiện title vào phần tử .

Chủ Đề