Có một số điều đang chơi ở đây. Như Fiprojects chỉ ra, tốt nhất không nên làm JavaScript nội tuyến [một số lý do chỉ là sở thích cá nhân]. Cuối cùng bạn sẽ lặp lại và làm cho việc duy trì mã của mình [trong số các lý do khác]. Đặt cược tốt nhất của bạn là sử dụng Event Listeners
[liên kết W3Schools, không phải lúc nào cũng là tài nguyên tốt nhất, nhưng đủ cho ví dụ này]. Đây là cực kỳ đơn giản nếu bạn đang sử dụng thư viện JavaScript [jQuery]. Nhưng là bạn đã yêu cầu một giải pháp JavaScript, tôi sẽ phác thảo cách thực hiện điều đó trong câu trả lời của tôi.
Đầu tiên, hãy định dạng mã của bạn để đọc dễ đọc hơn:
FIRST
SECOND
Tôi chỉ thực hiện một vài thay đổi ở đây. Tôi đã loại bỏ JavaScript Các thẻ được sử dụng để tạo các siêu liên kết trong HTML. Các phương thức sử dụng các biến bên trong thuộc tính ‘href này: Phần mềm này - Phần mềm này. title
. Tôi đã tạo một hình ảnh giữ chỗ [chỉ cho mục đích của riêng tôi, vì tôi không có hình ảnh của bạn, bạn sẽ muốn đặt hình ảnh của bạn trở lại đó]. Và cuối cùng, tôi đã thêm một class="myLink"
vào
GeeksforGeeks
This method uses the ‘ title’ property of ‘a’ tag,
i.e, whenever the link [‘a’ tag] is clicked, an ‘ title’ event is triggered.
Here we will use this title
event to generate a new URL and redirect the user to that URL.
[NOTE: This URL will contain the Variable we want to use inside href attribute]
First, we need to know the following terms,
Then we need to append the value to the URL.
Now our URL is ready with the variable and its value appended to it.
GeeksforGeeks
0
GeeksforGeeks1
GeeksforGeeks2
GeeksforGeeks3
GeeksforGeeks1
GeeksforGeeks5
GeeksforGeeks3
GeeksforGeeks7
GeeksforGeeks1
GeeksforGeeks9
Resultant Url: //www.google.com/?xyz=550
GeeksforGeeks9
GeeksforGeeks3
GeeksforGeeks7
GeeksforGeeks1
Resultant Url: //www.google.com/?xyz=555
GeeksforGeeks3
Resultant Url: //www.google.com/?xyz=557
Resultant Url: //www.google.com/?xyz=558
Khi chúng tôi có trò này. với nó.
Resultant Url: //www.google.com/?xyz=550
GeeksforGeeks5
GeeksforGeeks3
GeeksforGeeks7
GeeksforGeeks1
Resultant Url: //www.google.com/?xyz=558
Resultant Url: //www.google.com/?xyz=559
title
Event Listeners
1Event Listeners
2
GeeksforGeeks3
Event Listeners
4Event Listeners
5
Resultant Url: //www.google.com/?xyz=550
Event Listeners
7GeeksforGeeks3
GeeksforGeeks7
Resultant Url: //www.google.com/?xyz=550
Resultant Url: //www.google.com/?xyz=558
GeeksforGeeks3
Resultant Url: //www.google.com/?xyz=550
GeeksforGeeks2
GeeksforGeeks3
Resultant Url: //www.google.com/?xyz=55
Trong ví dụ dưới đây, chúng tôi sẽ nối một biến có tên ‘XYZ, và giá trị của nó là 55.
The URL has a variable named ‘XYZ’ that takes value = 55 from the javascript variable ‘val’.
GeeksforGeeks7
Resultant Url: //www.google.com/?xyz=550
Resultant Url: //www.google.com/?xyz=555
GeeksforGeeks3
document: When an HTML document is loaded into a web browser, it becomes a document object.
This document object has several functions, one of them is written [].
write[]: Writes HTML expressions or JavaScript code to a document
In this method, we will use this write[] function to create an “a tag”.
GeeksforGeeks0
GeeksforGeeks1
GeeksforGeeks2
GeeksforGeeks3
GeeksforGeeks1
GeeksforGeeks5
GeeksforGeeks3
GeeksforGeeks7
GeeksforGeeks1
GeeksforGeeks9
Resultant Url: //www.google.com/?xyz=550
GeeksforGeeks9
GeeksforGeeks3
GeeksforGeeks7
GeeksforGeeks1
Resultant Url: //www.google.com/?xyz=555
GeeksforGeeks3
Resultant Url: //www.google.com/?xyz=557
Resultant Url: //www.google.com/?xyz=558
GeeksforGeeks7
Resultant Url: //www.google.com/?xyz=550
Resultant Url: //www.google.com/?xyz=555
GeeksforGeeks3
Resultant Url: //www.google.com/?xyz=550
GeeksforGeeks5
GeeksforGeeks3
GeeksforGeeks7
GeeksforGeeks1
Resultant Url: //www.google.com/?xyz=558
Resultant Url: //www.google.com/?xyz=559
Resultant Url: //www.google.com/?xyz=557
class="myLink"
7Resultant Url: //www.google.com/?xyz=557
GeeksforGeeks1
Resultant Url: //www.google.com/?xyz=555
GeeksforGeeks3
Val Val là biến JavaScript lưu trữ giá trị mà chúng tôi muốn truyền vào URL. URL có một biến có tên ‘XYZ, có giá trị = 55 từ biến JavaScript‘ Val.
Resultant Url: //www.google.com/?xyz=557
Resultant Url: //www.google.com/?xyz=550
Resultant Url: //www.google.com/?xyz=555
GeeksforGeeks3
GeeksforGeeks7
Resultant Url: //www.google.com/?xyz=550
Resultant Url: //www.google.com/?xyz=558
GeeksforGeeks3
Resultant Url: //www.google.com/?xyz=550
GeeksforGeeks2
GeeksforGeeks3
Resultant Url: //www.google.com/?xyz=55
Sử dụng Document.Write: Document: Khi tài liệu HTML được tải vào trình duyệt web, nó trở thành đối tượng tài liệu. Đối tượng tài liệu này có một số chức năng, một trong số chúng được viết []. Một tài liệu trong phương thức này, chúng tôi sẽ sử dụng hàm write [] này để tạo một thẻ A AG.
The URL has a variable named ‘XYZ’ that takes value = 55 from the javascript variable val.