建模后:

function appendPre(message) {
    var pre = document.getElementById('content');
    var textContent = document.createTextNode(message + '\n');
    pre.appendChild(textContent);
}

appendPre('Files:');


如何制作允许我使用appendLink打印链接的appendLink('link')函数?

这是我到目前为止的内容:

function appendLink(url) {
  var link = document.createElement('a');
  var textContent = document.createTextNode(url + '\n');
  link.appendChild(textContent);
  link.href = 'test.com';
}

最佳答案

这是一个从给定的urltext生成链接(锚元素)的函数:

function link (url, text) {
  var a = document.createElement('a')
  a.href = url
  a.textContent = text || url
  return a
}


这是将其集成到表中的方法(以我对your other question的回答为基础):



function link (url, text) {
  var a = document.createElement('a')
  a.href = url
  a.textContent = text || url
  return a
}

function appendRow (table, elements, tag) {
  var row = document.createElement('tr')
  elements.forEach(function(e) {
    var cell = document.createElement(tag || 'td')
    if (typeof e === 'string') {
      cell.textContent = e
    } else {
      cell.appendChild(e)
    }

    row.appendChild(cell)
  })
  table.appendChild(row)
}


var file = {
  name: 'hello',
  viewedByMeTime: '2017-03-11T01:40:31.000Z',
  webViewLink: 'http://drive.google.com/134ksdf014kldsfi0234lkjdsf0314/',
  quotaBytesUsed: 0
}


var table = document.getElementById('content')

// Create header row
appendRow(table, ['Name', 'Date', 'Link', 'Size'], 'th')

// Create data row
appendRow(table, [
  file.name,
  file.viewedByMeTime.split('.')[0],
  link(file.webViewLink), // Note the enclosing call to `link`
  file.quotaBytesUsed + ' bytes'
])

#content td,
#content th {
  border: 1px solid #000;
  padding: 0.5em;
}

#content {
  border-collapse: collapse;
}

<table id="content">
</table>

关于javascript - 如何在JavaScript中设置`appendLink`?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42730172/

10-09 18:11