Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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
如何使用JQuery将链接放置到表单元格_Jquery_Html - Fatal编程技术网

如何使用JQuery将链接放置到表单元格

如何使用JQuery将链接放置到表单元格,jquery,html,Jquery,Html,下面是简单/示例表。 因为问题是如何为我需要的特定表格单元格设置链接。 例如,当我单击表第1行的第一个单元格“单元格1”时,它将执行链接并跳转到下一个站点 谢谢分享 您需要标签: 只需将href值替换为您尝试链接到的任何站点即可 jQuery的更新: 如果您正试图使用jQuery执行此操作,则首先需要向所需的td添加某种唯一标识符/选择器,例如类或序号位置,然后添加锚标记。我们现在就叫td选择“yourTd”: var aTag = $('<a>', {href: 'example

下面是简单/示例表。 因为问题是如何为我需要的特定表格单元格设置链接。 例如,当我单击表第1行的第一个单元格“单元格1”时,它将执行链接并跳转到下一个站点

谢谢分享

您需要标签:

只需将href值替换为您尝试链接到的任何站点即可

jQuery的更新:

如果您正试图使用jQuery执行此操作,则首先需要向所需的td添加某种唯一标识符/选择器,例如类或序号位置,然后添加锚标记。我们现在就叫td选择“yourTd”:

 var aTag = $('<a>', {href: 'example.com' });
 aTag.text($('.yourTd').text());// Populate the text with what's already there

 $('.yourTd').text('').append(aTag);// Remove the original text so it doesn't show twice.

以下是JQuery的实现方法:-

$('<a>',{
    text:'row 1, cell 1',
    href:'http://www.google.com'       
}).appendTo('tr:first td:nth-child(1) ');
HTML:-

<table border="1">
 <tr>
 <td></td>
 <td>row 1, cell 2</td>
 </tr>
 <tr>
 <td>row 2, cell 1</td>
 <td>row 2, cell 2</td>
 </tr>
 </table> 

您可以为指定的表单元格指定一个id(如链接器),然后添加一个单击事件处理程序

jQuery:

HTML:


首先在要在其上创建链接的html td元素上添加类,例如:

<table border="1">
  <tr>
    <td class="link">row 1, cell 1</td>
    <td>row 1, cell 2</td>
  </tr>
  <tr>
    <td class="link">row 2, cell 1</td>
    <td>row 2, cell 2</td>
  </tr>
</table>

如果您希望能够覆盖td内的链路,请使用以下代码。如果要通过ajax添加其他记录,也可以使用$body

<table class="table">
<tbody>
<tr data-url="/yourlink">
<td>test</td>
<td>test2</td>
<td class="custom_url">
<a href="youroverridelink">link</a>
</td>
</tr>
</tbody>
</table>


$(document).ready(function() {
   $('body').on('click','.table > tbody > tr > td', function() {
        window.location = $(this).not('.custom_url').parent().data('url');
   });
});

您试图执行的链接是什么?将锚定标记放在两个单元格之间我看不出这是一个JQuery问题。您是试图通过单击更改页面,还是将窗口移动到下一个表单元格?@koerbcm-Yup..我试图在单击单元格后更改页面。问题是关于如何使用JQuery添加链接。我假设他已经构建了表结构,需要动态添加链接。
$("td#linker").click(function(e)
{
    // Make your content bold
    $(this).css("font-weight","bold");
    // Direct the page like a link
    window.location.href="<WHER YOU WANT IT TO GO>";
    // Prevent the click from going up the chain
    e.stopPropagation();
});
<table border="1">
    <tr>
        <td id="linker">Click Me</td>
        <td>row 1, cell 2</td>
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table>
<table border="1">
  <tr>
    <td class="link">row 1, cell 1</td>
    <td>row 1, cell 2</td>
  </tr>
  <tr>
    <td class="link">row 2, cell 1</td>
    <td>row 2, cell 2</td>
  </tr>
</table>
$('td.link').each(function() {
    $(this).html('<a href="google.com">' + $(this).html() + '</a>')
});
<table class="table">
<tbody>
<tr data-url="/yourlink">
<td>test</td>
<td>test2</td>
<td class="custom_url">
<a href="youroverridelink">link</a>
</td>
</tr>
</tbody>
</table>


$(document).ready(function() {
   $('body').on('click','.table > tbody > tr > td', function() {
        window.location = $(this).not('.custom_url').parent().data('url');
   });
});