简体   繁体   English

将链接属性设置为不起作用的元素,无法弄清原因

[英]Setting Link Attribute to an element not working and can't figure out why

I am trying to set a link attribute to a field of a table, I think I am following the right steps, but I am missing something. 我正在尝试将链接属性设置为表的字段,我想我正在按照正确的步骤进行操作,但是却缺少一些内容。

I have this function that creates a table out of VAR data . 我有此功能,可以根据VAR数据创建表。 And What I wanted is to to transform the "first_name" field into a link (taken from the 'url' field of the object) 我想要的是将“ first_name”字段转换为链接(取自对象的“ url”字段)

 function createTable(data, field) { var tbody = document.getElementById("senate-data") for (var i = 0; i < data.length; i++) { var row = document.createElement("tr"); var cell1 = document.createElement("td"); var cell2 = document.createElement("td"); var cell3 = document.createElement("td"); var cell4 = document.createElement("td"); var cell5 = document.createElement("td"); var nameLink = document.createElement('a'); //created this var to later set the atrribute cell1.innerHTML = data[i].first_name; cell2.innerHTML = data[i].party; cell3.innerHTML = data[i].state; cell4.innerHTML = data[i].seniority; cell5.innerHTML = data[i].votes_with_party_pct; nameLink.setAttribute('href', data[i].url); cell1.appendChild(nameLink);//here i try to apend the var which is already a link, to the 'td' element ,getting the name turned into a link row.appendChild(cell1); row.appendChild(cell2); row.appendChild(cell3); row.appendChild(cell4); row.appendChild(cell5); tbody.appendChild(row);; } } createTable(data.results[0].members, ["first_name", "party", "state", "seniority", "votes_with_party_pct"] ); 

It is not working, and I don't know if it is just a small detail i am missing, or what I am trying is completely wrong. 它不起作用,我不知道这只是我丢失的一个小细节,还是我尝试的完全错误。

pd: i am begging with javascript, so If my method is not right, i would be looking for something easy for my level... pd:我正在用javascript请求,所以如果我的方法不正确,我会为我的水平寻找简单的方法...

As per my comment, you have to add actual text to the hyperlink for it to be clickable. 根据我的评论,您必须将实际文本添加到超链接中才能单击。 The href attribute only specifies the url to visit on click, not the text that will be shown as the hyperlink. href属性仅指定点击时访问的URL,而不指定将显示为超链接的文本。

 var data = { results: [ { members: [ { first_name: 'John', party: 'Dems', state: 'AZ', seniority: 1, votes_with_party_pct: 37, url: '#john' }, { first_name: 'Jane', party: 'Reps', state: 'TX', seniority: 1, votes_with_party_pct: 15, url: '#jane' }, { first_name: 'Bob', party: 'Greens', state: 'CA', seniority: 2, votes_with_party_pct: 38, url: '#bob'}, { first_name: 'Alice', party: 'Indeps', state: 'NY', seniority: 3, votes_with_party_pct: 14, url: '#alice' } ] } ] }; function createTable(data, field) { var tbody = document.getElementById("senate-data") for (var i = 0; i < data.length; i++) { var row = document.createElement("tr"); var cell1 = document.createElement("td"); var cell2 = document.createElement("td"); var cell3 = document.createElement("td"); var cell4 = document.createElement("td"); var cell5 = document.createElement("td"); var nameLink = document.createElement('a'); //created this var to later set the atrribute // Add the name to the hyperlink instead. nameLink.appendChild( document.createTextNode( data[i].first_name )); nameLink.setAttribute('href', data[i].url); // Append the hyperlink to the cell. The name is now clickable as a hyperlink. cell1.appendChild(nameLink); cell2.innerHTML = data[i].party; cell3.innerHTML = data[i].state; cell4.innerHTML = data[i].seniority; cell5.innerHTML = data[i].votes_with_party_pct; row.appendChild(cell1); row.appendChild(cell2); row.appendChild(cell3); row.appendChild(cell4); row.appendChild(cell5); tbody.appendChild(row);; } } createTable(data.results[0].members, ["first_name", "party", "state", "seniority", "votes_with_party_pct"] ); 
 <table id="senate-data"></table> 

PS: You're not using your field array. PS:您没有使用field数组。 I guess the intention is to loop over it instead to create the table so you do not have to change the createTable function if a field name disappears, changes name, or is added. 我想本来是要遍历它而不是创建表,所以如果字段名称消失,更改名称或添加,则不必更改createTable函数。

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

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