javascript - 除最后一个 <td> 元素之外的 Fire Click 函数 - Jquery

标签 javascript php jquery

我在 php 文档中有这个表

    <?php
    echo "
    <table id='supplier-detail-table' class='table table-responsive table-hover'>
            <tr><th>Supplier Name</th><th>Address</th><th>Phone</th><th>Fax</th><th>Email</th><th>Website</th></tr>";
                foreach ($all_suppliers as $supplier){
                    echo "<tr data-id=".$supplier['id']."><td class='text-center'>".$supplier['name']."</td><td>".$supplier['address']."</td><td>".$supplier['phone']."</td><td>".$supplier['fax']."</td><td>".$supplier['email']."</td><td><a href='".$supplier['url']."' target='_blank'>".$supplier['url']."</td></tr>"; 
    echo "</table>";
?>

我希望表行可单击,以便当具有特定 data-id 的行时单击后,用户将看到 vendor 的完整详细信息

JS:

$(document).ready(function(){
    $("#supplier-detail-table tr").click(function(){
        if($(this).data("id")){
            window.location="http://localhost/nams/index.php/View/Supplier/"+$(this).data("id");
        }
    })
});

现在,如您所见,如果单击该行,用户将进入有关 vendor 的完整详细信息页面。

但是最后一个td元素,我将其放置为 <a></a> 。因此,当用户单击网站时,会加载带有 url 的空白选项卡,并且当前页面也会更改为完整的详细信息页面。

如何防止这种情况发生?

如果用户点击td:last-child ,点击功能应该不起作用!这样就会打开空白页面,并且该页面不应加载到完整详细信息页面。

最佳答案

尝试

$("table tr")
.on("click", function(e) {
  if ($(this).is("tr:last")) {
    e.preventDefault();
    e.stopPropagation();
    return false
  };

  // do stuff
});

$("table tr")
.on("click", function(e) {

  if ($(this).is("tr:last")) {
    e.preventDefault();
    e.stopPropagation();
    return false
  }
  
  console.log($(this).text())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <tr>
    <td>a</td>
  </tr>
    <tr>
    <td>b</td>
  </tr>
    <tr>
    <td>c</td>
  </tr>
    <tr>
    <td><a href="http://stackoverflow.com/questions/28796309/fire-click-function-except-for-the-last-td-element-jquery/">123</a></td>
  </tr>
  </table>

关于javascript - 除最后一个 <td> 元素之外的 Fire Click 函数 - Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28796309/

相关文章:

javascript - spin.js 启动但不停止

javascript - 使用 .effect 时 DIV 跳转 ("shake")

javascript - React-Router 查询字符串比状态变量落后一步

javascript - 覆盖 d3 TypeScript 定义文件

php - 如何增加和减少网页中的文字大小?

php - 如何在mysql中编写条件选择查询

php - 从特定 id 名称开始的 DOM 中获取 div 数据

javascript - Chrome 扩展 - 如何将值传递到后台页面

javascript - Blueimp 画廊用于 Bootstrap 多个画廊问题

javascript - 查询 : Understanding Syntax