如何将createTextNode()的值转换为链接? [英] how to turn value of createTextNode() to a link?
问题描述
我有一个filter方法,该方法返回数组的键和值.现在我正在获取完整的URL,但是我需要将其包装在标记中,以便可以将URL传递给它的href.过滤方法:
I have a filter method that returns an array's keys and values. Right now I am getting the full url but I need it wrapped in tag so I can pass the url to its href. The filter method:
let filtered = myData.map(row =>
Object.fromEntries(
Object.entries(row).filter(it => {
let key = it[0];
return ['Carat','Clarity','Shape', 'Color', 'Cut','Symmetry','Report', 'view' ].indexOf(key) >= 0
})
)
);
view的值(它是return语句中的最后一个元素)提供了一个url.当我执行以下操作来生成表时,我得到了键和值,但是如何选择该视图(这是一个链接)并将其包装在标签中并放置在href中?
view's value which is the last element in return statement gives a url. when I do the following to generate my table I get keys and values but how do I select that view (which is a link) and wrap it in a tag and place view in href?
function generateTableHead(table, data) {
let thead = table.createTHead();
let row = thead.insertRow();
for (let key of data) {
let th = document.createElement("th");
let text = document.createTextNode(key);
// casual formalities
th.appendChild(text);
row.appendChild(th);
}
}
function generateTable(table, data) {
for (let element of data) {
let row = table.insertRow();
for (key in element) {
let cell = row.insertCell();
let text = document.createTextNode(element[key]);
cell.appendChild(text);
}
}
}
let table = document.querySelector("table");
// create a variable from values
generateTable(table, filtered);
// create variable from keys
generateTableHead(table, headArr);
推荐答案
采用JS中的值,并使用innerHTML将html元素设置为href元素的模板文字...
take the value in JS and use innerHTML to set an html element to a template literal of the href element...
例如
//JS
let link = "https://yoururl.com/path/thingy";
let targetElement = document.getElementById("myDiv");
targetElement.innerHTML = `<a href=${link}>thingy</a>`;
这篇关于如何将createTextNode()的值转换为链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!