英文:
jQuery add link to text in every row cell from url on another cell
问题
我有一个谷歌表格,里面有很多来自特别优惠的条目的表格,使用 CSV 导出到我的网站,然后我在表格中显示这些优惠。
我的问题是,CSV 导出不包括标题中船只的链接,所以我已经将链接添加到谷歌表格表的末尾,这样我可以使用 jQuery 获取单元格的值并将船只标题的文本替换为包含船只名称和链接的文本。
这是谷歌表格。我想要获取的 URL 在I列;然后我用B列的原始文本替换具有来自I列链接的原始文本。
它确实有点奏效,但它将每个单元格中的所有链接都放在<a href>标签中,如下所示:
这是我需要修改的代码:
<script>
jQuery(document).ready(function($) {
$('#create_tables td:nth-child(2)').html(function(){
return '<a href="' + $('#create_tables td:nth-child(9)').text() + '" target="_blank">' + $(this).text() + '</a>';
});
});
</script>
英文:
I have a Google Sheet that has a table of many entries from Special Offers that are loaded using csv export to my site and then I display those offers in a table.
My problem is that csv export does not include the link of the boat that's in the title, so I have added the link at the end of the google sheet table so I can then use jquery to get the value of the cell and replace the text of the boat title to include the boat name along with the link.
Here's the google sheet. The URL I want to get is at the I column; and I then replace the text at the B column with the original text that has the link from column I.
It does work kinda, but it puts all the links from every cell in the <a href> tag like so:
Here's the code I use that needs revamp:
<script>
jQuery(document).ready(function($) {
$('#create_tables td:nth-child(2)').html(function(){
return '<a href="' + $('#create_tables td:nth-child(9)').text() + '" target="_blank">' + $(this).text() + '</a>';
});
});
</script>
答案1
得分: 1
以下是翻译好的代码部分:
$('#create_tables td:nth-child(2)').each(function(){
const $row = $(this).closest("tr");
const url = $row.find("td").eq(8).text(); // nth-child is 1 based, .eq 0 based
$(this).html(`<a href="${url}" target="_blank">${this.textContent}</a>`);
})
如果您需要更多的翻译,请告诉我。
英文:
You need each and closest to navigate from one cell to a sibling cell
$('#create_tables td:nth-child(2)').each(function(){
const $row = $(this).closest("tr");
const url = $row.find("td").eq(8).text(); // nth-child is 1 based, .eq 0 based
$(this).html(`<a href="${url}" target="_blank">${this.textContent}</a>`);
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论