Javascript JQuery基于锚定标记的文本制作超链接
我有下面的表格&根据表格中的文本,使表格的单元格可单击/超链接的最佳方法是什么Javascript JQuery基于锚定标记的文本制作超链接,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我有下面的表格&根据表格中的文本,使表格的单元格可单击/超链接的最佳方法是什么 <table id="fresh-table" class="table"> <thead> <th data-field="id" data-sortable="true">ID</th> <th data-field="URL" data-sortable="true">URL</th>
<table id="fresh-table" class="table">
<thead>
<th data-field="id" data-sortable="true">ID</th>
<th data-field="URL" data-sortable="true">URL</th>
<th data-field="Results">Results</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="#">https://google.com</td>
<td>Woot</td>
</tr>
<tr>
<td>1</td>
<td><a href="#">https://facebook.com</td>
<td>Hax</td>
</tr>
</tbody>
</table>
我想根据得到的文本替换href
的值,例如:
https://google.com
或https://facebook.com
首先,请注意您的HTML是无效的;您缺少用于关闭
表中锚定的
标记
其次,jQuery没有getText()
方法。我想你是想用它来代替
关于您的问题,您可以使用prop()
将a
元素的href
属性设置为与其文本()相等。最简单的方法是为prop()
提供一个函数,该函数将在集合中的每个元素上执行。试试这个:
$('fresh table a').prop('href',function(){
返回$(this.text();
});代码>
身份证件
统一资源定位地址
结果
1.
胡特
1.
哈克斯
无需jQuery,只需几行代码即可实现:
document.addEventListener("DOMContentLoaded", () => {
for (const element of document.querySelectorAll("a[href='#']")) {
element.href = element.innerText;
}
});
没问题,很乐意帮忙