简体   繁体   English

在 Javascript 中添加指向 .textcontent 的链接

[英]add link to .textcontent in Javascript

When I received help on adding a link to symbolSpan, I am now getting a link but the symbols are showing at the bottom of the page and not in the table with the rest of the data... How do I fix that?当我收到有关添加指向 symbolSpan 的链接的帮助时,我现在得到了一个链接,但这些符号显示在页面底部,而不是在带有数据 rest 的表格中......我该如何解决这个问题?

const renderBalances = balances => {
  //
  binanceBalances.innerHTML = "";
  //
  //console.log(balances);
  balances.forEach(balance => {
    let balanceLi = document.createElement("li");
    balanceLi.className = "list-group-item list-group-item-justify-content-center";
    let balanceDiv = document.createElement("div");
    balanceDiv.className = "d-flex w-100 justify-content-between"; 

        let symbolSpan = document.createElement("span");
        symbolSpan.textContent = balance.symbol;
     //   balanceDiv.appendChild(symbolSpan);
        function createLink(linkExtension) {
    
          const balanceDiv = document.createElement("div");
          const symbolSpan = document.createElement("span");
          const link = document.createElement("a");
        
          link.setAttribute('href', `www.binance.com/en/trade/${linkExtension}`);
          link.textContent = balance.symbol;
        
          symbolSpan.appendChild(link);
          balanceDiv.appendChild(symbolSpan);
          document.body.appendChild(balanceDiv);
        }
    createLink('myparam')

this is the rest of the code for this table这是此表代码的 rest

    let price = document.createElement("span");
    price.textContent = balance.startPrice;
    balanceDiv.appendChild(price);
    let available = document.createElement("span");
    available.textContent = balance.holdings;
    balanceDiv.appendChild(available);
    let onOrder = document.createElement("span");
    onOrder.textContent = balance.used;
    balanceDiv.appendChild(onOrder);

    balanceLi.appendChild(balanceDiv);

    binanceBalances.appendChild(balanceLi);
  });
};

Create a span and a link html element.创建一个跨度和一个链接 html 元素。 For the link element append the text and the href attribute.对于链接元素 append 的文本和 href 属性。

Attach the link element to span and span to div.将链接元素附加到 span 和 span 到 div。 At last append to DOM.最后 append 到 DOM。

Pass the additional parameter that needs to be appended to the link.传递需要附加到链接的附加参数。

 function createLink(linkExtension) { const balanceDiv = document.createElement("div"); const symbolSpan = document.createElement("span"); const link = document.createElement("a"); link.setAttribute('href', `www.binance.com/en/trade/${linkExtension}`); link.textContent = 'binance link'; symbolSpan.appendChild(link); balanceDiv.appendChild(symbolSpan); document.body.appendChild(balanceDiv); } createLink('myparam')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM