Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

Tôi có một chuỗi cho một tiêu đề và một chuỗi cho một liên kết. Tôi không chắc làm thế nào để đặt cả hai lại với nhau để tạo một liên kết trên một trang bằng JavaScript. Bất kỳ sự giúp đỡ được đánh giá cao.

EDIT1: Thêm chi tiết hơn vào câu hỏi. Lý do tôi đang cố gắng tìm ra điều này là vì tôi có nguồn cấp dữ liệu RSS và có một danh sách các URL tiêu đề ANDS. Tôi muốn liên kết các tiêu đề với URL để làm cho trang hữu ích.

EDIT2: Tôi đang sử dụng jQuery nhưng hoàn toàn mới với nó và không biết nó có thể giúp ích trong tình huống này.

hỏi ngày 23 tháng 1 năm 2011 lúc 7:37Jan 23, 2011 at 7:37

Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

XavierxavierXavier

8,58013 Huy hiệu vàng63 Huy hiệu bạc98 Huy hiệu Đồng13 gold badges63 silver badges98 bronze badges

2


  
  
    
  

Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

Harry Moreno

9.2996 Huy hiệu vàng59 Huy hiệu bạc105 Huy hiệu Đồng6 gold badges59 silver badges105 bronze badges

Đã trả lời ngày 23 tháng 1 năm 2011 lúc 7:46Jan 23, 2011 at 7:46

Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

Jared Farrishjared FarrishJared Farrish

47.6K17 Huy hiệu vàng94 Huy hiệu bạc101 Huy hiệu đồng17 gold badges94 silver badges101 bronze badges

3

Với JavaScript

  1. var a = document.createElement('a');
    a.setAttribute('href',desiredLink);
    a.innerHTML = desiredText;
    // apend the anchor to the body
    // of course you can append it almost to any other dom element
    document.getElementsByTagName('body')[0].appendChild(a);
    
  2. document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    

    Hoặc, như đề xuất của @Travis:

    document.getElementsByTagName('body')[0].innerHTML += desiredText.link(desiredLink);
    
  3. 
    

Với jQuery

  1. $(''+desiredText+'').appendTo($('body'));
    
  2. $('body').append($(''+desiredText+''));
    
  3. var a = $('');
    a.attr('href',desiredLink);
    a.text(desiredText);
    $('body').append(a);
    

Trong tất cả các ví dụ trên, bạn có thể nối neo vào bất kỳ phần tử nào, không chỉ vào 'cơ thể' và

var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
6 là một biến giữ địa chỉ mà phần tử neo của bạn chỉ ra và
var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
7 là một biến giữ văn bản sẽ hiển thị trong phần tử neo.

Đã trả lời ngày 23 tháng 1 năm 2011 lúc 8:50Jan 23, 2011 at 8:50

Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

gion_13gion_13gion_13

40.7k10 Huy hiệu vàng96 Huy hiệu bạc107 Huy hiệu đồng10 gold badges96 silver badges107 bronze badges

2

Tạo các liên kết bằng JavaScript:


HOẶC


HOẶC

var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
0

Đã trả lời ngày 23 tháng 1 năm 2011 lúc 7:42Jan 23, 2011 at 7:42

Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

NaveednaveedNaveed

40.7K32 Huy hiệu vàng95 Huy hiệu bạc130 Huy hiệu đồng32 gold badges95 silver badges130 bronze badges

Có một vài cách khác nhau:

Nếu bạn muốn sử dụng JavaScript thô (không có người trợ giúp như JQuery), thì bạn có thể làm một cái gì đó như:

var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
1

Phương pháp khác là ghi liên kết trực tiếp vào tài liệu:

var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
2

Đã trả lời ngày 23 tháng 1 năm 2011 lúc 7:52Jan 23, 2011 at 7:52

3

var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
3

  1. 'Đối tượng neo' có các thuộc tính* (kế thừa)* của riêng mình để đặt liên kết, văn bản của nó. Vì vậy, chỉ cần sử dụng chúng. .SetAttribution là chung chung hơn nhưng bạn thường không cần nó.

    var a = document.createElement('a');
    a.setAttribute('href',desiredLink);
    a.innerHTML = desiredText;
    // apend the anchor to the body
    // of course you can append it almost to any other dom element
    document.getElementsByTagName('body')[0].appendChild(a);
    
    8 sẽ làm như vậy và rõ ràng hơn! Vâng, một tình huống sẽ yêu cầu .SetAttribution là:
    var a = document.createElement('a');
    a.setAttribute('href',desiredLink);
    a.innerHTML = desiredText;
    // apend the anchor to the body
    // of course you can append it almost to any other dom element
    document.getElementsByTagName('body')[0].appendChild(a);
    
    9.setAttribute is more general but you normally don't need it.
    var a = document.createElement('a');
    a.setAttribute('href',desiredLink);
    a.innerHTML = desiredText;
    // apend the anchor to the body
    // of course you can append it almost to any other dom element
    document.getElementsByTagName('body')[0].appendChild(a);
    
    8 will do the same and is more clear! Well a situation that'll demand .setAttribute is this:
    var a = document.createElement('a');
    a.setAttribute('href',desiredLink);
    a.innerHTML = desiredText;
    // apend the anchor to the body
    // of course you can append it almost to any other dom element
    document.getElementsByTagName('body')[0].appendChild(a);
    
    9

  2. Để lại giao thức mở. Thay vì http://example.com/path xem xét chỉ sử dụng //example.com/path. Kiểm tra xem espert.com có ​​thể được truy cập bởi HTTP không: cũng như HTTPS: nhưng 95 % trang web sẽ hoạt động trên cả hai.http://example.com/path consider to just use //example.com/path. Check if example.com can be accessed by http: as well as https: but 95 % of sites will work on both.

  3. Offtopic: Điều đó không thực sự liên quan đến việc tạo các liên kết trong JS nhưng có lẽ tốt để biết: đôi khi giống như trong các bộ giảm giá của bộ định hướng bạn có thể sử dụng ____2020 thay vì

    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    1
    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    2 sẽ 'danh dự' những nỗ lực của bạn với lỗi gọi bất hợp pháp ngay lần đầu tiên bạn sử dụng nó. Đó là bởi vì nhiệm vụ chỉ 'lấy' .QuerySelector (một ref cho phương thức lớp). Với
    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    3, bạn cũng sẽ liên quan đến bối cảnh (ở đây là
    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    4) và bạn nhận được một phương thức đối tượng sẽ hoạt động như bạn có thể mong đợi.
    That's not really relevant about creating links in JS but maybe good to know: Well sometimes like in the chromes dev-console you can use
    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    0 instead of
    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    1 A
    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    2will 'honor' your efforts with an Illegal invocation error the first time you use it. That's because the assignment just 'grabs' .querySelector (a ref to the class method). With
    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    3 you'll also involve the context (here it's
    document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
    
    4) and you get an object method that'll work as you might expect it.

Đã trả lời ngày 1 tháng 2 năm 2017 lúc 17:04Feb 1, 2017 at 17:04

Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

NadunaduNadu

2.2841 Huy hiệu vàng23 Huy hiệu bạc29 Huy hiệu đồng1 gold badge23 silver badges29 bronze badges

Tự động tạo một siêu liên kết với JavaScript thô:

var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
4

Đã trả lời ngày 23 tháng 1 năm 2011 lúc 7:46Jan 23, 2011 at 7:46

Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

Jared Farrishjared Farrishjamesmortensen

47.6K17 Huy hiệu vàng94 Huy hiệu bạc101 Huy hiệu đồng11 gold badges95 silver badges118 bronze badges

1

Với JavaScriptquick way to create elements:

var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
5

Hoặc, như đề xuất của @Travis:Jun 4 at 16:52

Hướng dẫn how do you append a link in javascript? - làm thế nào để bạn nối một liên kết trong javascript?

Với jQueryGorvGoyl

Trong tất cả các ví dụ trên, bạn có thể nối neo vào bất kỳ phần tử nào, không chỉ vào 'cơ thể' và

var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
6 là một biến giữ địa chỉ mà phần tử neo của bạn chỉ ra và
var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
7 là một biến giữ văn bản sẽ hiển thị trong phần tử neo.24 gold badges197 silver badges193 bronze badges

Đã trả lời ngày 23 tháng 1 năm 2011 lúc 8:50

document.getElementsByTagName('body')[0].innerHTML += ''+desiredText+'';
5

40.7k10 Huy hiệu vàng96 Huy hiệu bạc107 Huy hiệu đồngApr 24, 2016 at 18:11

1

Làm cách nào để nối một liên kết?

Nhấn Ctrl+K.Bạn cũng có thể nhấp chuột phải vào văn bản hoặc hình ảnh và nhấp vào liên kết trên menu phím tắt.Trong hộp Chèn Hộp siêu liên kết, nhập hoặc dán liên kết của bạn vào hộp địa chỉ.Lưu ý: Nếu bạn không thấy hộp địa chỉ, hãy đảm bảo tệp hoặc trang web hiện có được chọn trong liên kết đến.. You can also right-click the text or picture and click Link on the shortcut menu. In the Insert Hyperlink box, type or paste your link in the Address box. Note: If you don't see the Address box, make sure Existing File or Web Page is selected under Link to.

Làm thế nào để bạn liên kết một chuỗi trong javascript?

Phương thức liên kết () tạo một chuỗi nhúng một chuỗi vào một phần tử (STR), được sử dụng làm liên kết siêu văn bản vào URL khác. creates a string that embeds a string in an element ( str ), to be used as a hypertext link to another URL.

Làm thế nào để bạn nhúng một liên kết trong HTML?

Để tạo một siêu liên kết trong trang HTML, hãy sử dụng các thẻ và các thẻ, là các thẻ được sử dụng để xác định các liên kết.Thẻ cho biết nơi siêu liên kết khởi động và thẻ cho biết nơi nó kết thúc.Bất cứ văn bản nào được thêm vào bên trong các thẻ này, sẽ hoạt động như một siêu liên kết.Thêm URL cho liên kết trong.use the and tags, which are the tags used to define the links. The tag indicates where the hyperlink starts and the tag indicates where it ends. Whatever text gets added inside these tags, will work as a hyperlink. Add the URL for the link in the .

Làm thế nào để bạn liên kết các đối tượng trong JavaScript?

JavaScript hợp nhất các đối tượng để hợp nhất các đối tượng thành một đối tượng mới có tất cả các thuộc tính của các đối tượng được hợp nhất, bạn có hai tùy chọn: sử dụng toán tử lây lan (...) Sử dụng đối tượng.Phương thức gán ().Use a spread operator ( ... ) Use the Object. assign() method.