jQuery在每个行单元格中的文本中添加链接,链接的URL来自另一个单元格。

huangapple go评论52阅读模式
英文:

jQuery add link to text in every row cell from url on another cell

问题

我有一个谷歌表格,里面有很多来自特别优惠的条目的表格,使用 CSV 导出到我的网站,然后我在表格中显示这些优惠。

我的问题是,CSV 导出不包括标题中船只的链接,所以我已经将链接添加到谷歌表格表的末尾,这样我可以使用 jQuery 获取单元格的值并将船只标题的文本替换为包含船只名称和链接的文本。

这是谷歌表格。我想要获取的 URL 在I列;然后我用B列的原始文本替换具有来自I列链接的原始文本。
jQuery在每个行单元格中的文本中添加链接,链接的URL来自另一个单元格。

它确实有点奏效,但它将每个单元格中的所有链接都放在<a href>标签中,如下所示:
jQuery在每个行单元格中的文本中添加链接,链接的URL来自另一个单元格。

这是我需要修改的代码:

<script>
jQuery(document).ready(function($) {
	$('#create_tables td:nth-child(2)').html(function(){
		return '&lt;a href=&quot;' + $('#create_tables td:nth-child(9)').text() + '&quot; target=&quot;_blank&quot;&gt;' + $(this).text() + '&lt;/a&gt;';
	});
});
</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.
jQuery在每个行单元格中的文本中添加链接,链接的URL来自另一个单元格。

It does work kinda, but it puts all the links from every cell in the <a href> tag like so:
jQuery在每个行单元格中的文本中添加链接,链接的URL来自另一个单元格。

Here's the code I use that needs revamp:

&lt;script&gt;
jQuery(document).ready(function($) {
	$(&#39;#create_tables td:nth-child(2)&#39;).html(function(){
		return &#39;&lt;a href=&quot;&#39; + $(&#39;#create_tables td:nth-child(9)&#39;).text() + &#39;&quot; target=&quot;_blank&quot;&gt;&#39; + $(this).text() + &#39;&lt;/a&gt;&#39;;
	});
});
&lt;/script&gt;

答案1

得分: 1

以下是翻译好的代码部分:

$(&#39;#create_tables td:nth-child(2)&#39;).each(function(){
  const $row = $(this).closest(&quot;tr&quot;);
  const url = $row.find(&quot;td&quot;).eq(8).text(); // nth-child is 1 based, .eq 0 based
  $(this).html(`&lt;a href=&quot;${url}&quot; target=&quot;_blank&quot;&gt;${this.textContent}&lt;/a&gt;`);
})

如果您需要更多的翻译,请告诉我。

英文:

You need each and closest to navigate from one cell to a sibling cell

$(&#39;#create_tables td:nth-child(2)&#39;).each(function(){
  const $row = $(this).closest(&quot;tr&quot;);
  const url = $row.find(&quot;td&quot;).eq(8).text(); // nth-child is 1 based, .eq 0 based
  $(this).html(`&lt;a href=&quot;${url}&quot; target=&quot;_blank&quot;&gt;${this.textContent}&lt;/a&gt;`);
})

huangapple
  • 本文由 发表于 2023年5月17日 16:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定