HTML DOM cho phép JavaScript thay đổi nội dung của các phần tử HTML
Thay đổi nội dung HTML
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
Để thay đổi nội dung của một phần tử HTML, hãy sử dụng cú pháp này
tài liệu. getElementById(id). InternalHTML = HTML mới
Ví dụ này thay đổi nội dung của một
yếu tố
Thí dụ
Chào thế giới
Ví dụ giải thích
- Tài liệu HTML ở trên chứa một
phần tử có 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. "
Ví dụ này thay đổi nội dung của một
yếu tốThí dụ
Ví dụ giải thích
- Tài liệu HTML ở trên chứa một phần tử có id="id01"
- Chúng tôi sử dụng HTML DOM để lấy phần tử có id="id01"
- JavaScript thay đổi nội dung (innerHTML) của phần tử đó thành "Tiêu đề mới"
Thay đổi giá trị của một thuộc tính
Để thay đổi giá trị của thuộc tính HTML, hãy sử dụng cú pháp này
tài liệu. getElementById(id). thuộc tính = giá trị mới
Ví dụ này thay đổi giá trị của thuộc tính src của phần tử <html> <head> <script src=//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js> </script> </head> <body> <div id="demo"> <div class="name"> ---Datas with html tags if needed --- </div> </div> <script> $("name").click(function(e){ e.preventDefault(); var eg=; $('#demo').replaceWith('<div class="name">' + eg + '</div>'); }); </script> </body> </html>2
Thí dụ
Ví dụ giải thích
- Tài liệu HTML ở trên chứa một phần tử <html> <head> <script src=//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js> </script> </head> <body> <div id="demo"> <div class="name"> ---Datas with html tags if needed --- </div> </div> <script> $("name").click(function(e){ e.preventDefault(); var eg=; $('#demo').replaceWith('<div class="name">' + eg + '</div>'); }); </script> </body> </html>2 với innerHTML0
- Chúng tôi sử dụng HTML DOM để lấy phần tử có innerHTML0
- Một JavaScript thay đổi thuộc tính innerHTML2 của phần tử đó từ "smiley. gif" thành "phong cảnh. jpg"
Nội dung HTML động
JavaScript có thể tạo nội dung HTML động
Thí dụ
tài liệu. viết()
Trong JavaScript, innerHTML3 có thể được sử dụng để ghi trực tiếp vào luồng đầu ra HTML
Thí dụ
bla bla bla
bla bla bla
Không bao giờ sử dụng innerHTML3 sau khi tải tài liệu. Nó sẽ ghi đè lên tài liệu
Kiểm tra bản thân với các bài tập
Tập thể dục
Sử dụng HTML DOM để thay đổi giá trị của thuộc tính src của hình ảnh
Cung cấp câu trả lời "
bắt đầu bài tập
The jQuery replace div contents is the data contents that can be used on the
cú pháp
Bắt đầu khóa học phát triển phần mềm miễn phí của bạn
Phát triển web, ngôn ngữ lập trình, kiểm thử phần mềm và những thứ khác
The jQuery library has many default methods and classes, widgets for used on creating the web based application. The HTML will combine with jQuery for adding the data contents on the both script and UI code. Mostly the
The above codes are the basic syntax for utilising the
Làm cách nào để thay thế nội dung div trong jQuery?
The above paragraph in syntax section we discussed on the
Ví dụ về jQuery thay thế nội dung div
Dưới đây là các ví dụ sau được đề cập dưới đây
Gói phát triển phần mềm tất cả trong một(hơn 600 khóa học, hơn 50 dự án)
Giá
Xem khóa học
600+ Khóa học trực tuyến. hơn 50 dự án. Hơn 3000 giờ. Giấy chứng nhận có thể kiểm chứng. Truy cập Trọn đời
4. 6 (82.404 xếp hạng)
Ví dụ 1
Mã số
<!DOCTYPE html> <html> <head> <title> Welcome to My Domain it’s a first Example for div tag using jQuery Replace concept</title> <style> body { display: block; margin-top: 17%; } h1 { color:blue; text-align:center; } div { border:3px green; color:violet; margin:7px; cursor:pointer; } p { border:3px green; color:blue; margin:3px; cursor:pointer; } </style> <script src="//code.jquery.com/jquery-latest.js"></script> </head> <body bgcolor="red"> <p>Have a Nice Day Users</p> <p>Thank you spending you time in our application kindly stay on this</p> <p>Please find the below our Updates on this application</p> <p>The div tag data contents are used and shown on the screen by using the html web pages</p> <p>With the help of jQuery replaceWith() method the datas are replaced with the new html dom elements</p> <div>Your replaced contents will be shown on the web screen</div> <script> $("p").click(function () { $(this).replaceWith($("div")); }); </script> </body> </html>đầu ra
Trong ví dụ trên, chúng tôi đã sử dụng nội dung div với phương thức replaceWith() để thay thế một phần tử dom bằng một phần tử khác
Ví dụ #2
Mã số
<html xmlns="//www.w3.org/1999/xhtml" > <head> <title>Welcome To My Domain its a Second Example for replacing the elements by using the div tag elements</title> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script> <style> body { font-family: Arial, TimesNewRoman,Helvetica, sans-serif; background-color: blue; color: green; text-decoration: none; padding: 23px 33px; position: center; display: inline-block; border-radius: 5px; } .demo { position: center; top: -23px; right: -23px; padding: 7px 13px; border-radius: 47%; background-color: pink; color: yellow; } </style> <script type="text/javascript" language="javascript"> $(function () { $("#first").click(function () { $("#demo").html($("#demo").html().replace("Welcome", "raman")); }); }); </script> </head> <body> <div id="demo">Welcome <div class=”name”> Welcome Users its the second example for replacing div contents by using the jQuery replace() method its one of the scenario we used </div> <div class="name"> Welcome Raman Have a Nice Day Thank you for spending the time for this occasion </div> <div class="name"> Welcome Siva Have a Nice Day Thank you for spending the time for this occasion </div> </div><br /> <input id="first" type="button" value="Replace Value" /> </body> </html>đầu ra
Trong ví dụ thứ hai, chúng tôi đã sử dụng nội dung html trên trang, ngoài ra, phần tử nút được sử dụng trên tập lệnh để thay thế nội dung cụ thể trên trang web
Ví dụ #3
Mã số
<!DOCTYPE html> <html> <head> <title>Welcome to My Domain it’s a Third Example for regarding the div contents on the replace method</title> <style> img { width: 540px; height: 530px; } </style> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script> <script src="//code.jquery.com/jquery-3.5.0.min.js"> </script> <script type="text/javascript" language="javascript"> $(function () { $("#first").click(function () { $("#demo").html($("#demo").html().replace("Welcome you", "raman")); }); }); </script> </head> <body> <div id="demo">Welcome you users <div class="name"> <img id="second" src='website.jpg' alt="values" /> <marquee>Thank you users have a nice day</marquee> </div> </div> <input id="first" type="button" value="Replace Value" /> <script> $(document).ready(function() { $('img').on({ 'click': function() { let vars = ($(this).attr('src') === 'one.jpg') ? 'website.jpg' : 'one.jpg'; $(this).attr('src', vars); } }); }); </script> </body> </html>đầu ra
Trong ví dụ cuối cùng, chúng tôi đã sử dụng các hình ảnh bổ sung trong tập lệnh để thay đổi hình ảnh khi chúng tôi nhấp vào hình ảnh trên trang. Ngoài ra, chúng tôi nhấp vào nút để thay thế nội dung
Sự kết luận
In jQuery library has default methods and keywords for creating a web application with user-friendly nature. Additionally, the html elements like
Bài viết được đề xuất
Đây là hướng dẫn về jQuery thay thế nội dung div. Ở đây chúng ta thảo luận về Cách thay thế nội dung div trong jQuery cùng với các ví dụ và kết quả đầu ra. Bạn cũng có thể xem các bài viết sau để tìm hiểu thêm –