Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery基于锚定标记的文本制作超链接_Javascript_Jquery_Jquery Plugins - Fatal编程技术网

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;
  }
});

没问题,很乐意帮忙