javascript - 在可点击的行内使 span 可点击

标签 javascript jquery html css

我有以下代码:

$(".clickable").click(function() {
  window.location = $(this).data("target");
});
$(".clickableB").click(function() {
  alert('I got a click');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
  <thead>
    <tr>
      <th>Employee</th>
      <th>Total hours</th>
      <th>Comments</th>
      <th>Options</th>
    </tr>
  </thead>
  <tbody>
    <tr data-toggle="modal" data-target="#events-modal" class="clickable success">
      <td>Pedro</td>
      <td>1</td>
      <td>This is a very loooooooooooooooooooong text</td>
      <td>
        <span style="color:green" class="clickableB fa fa-check-square"></span>
        <span style="color:orange" class="clickableB fa fa-warning"></span>
        <span style="color:red" class="clickableB fa fa-minus-square"></span>
      </td>
    </tr>
  </tbody>
</table>

<div class="modal fade" id="events-modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body" style="height: 400px">
                <p>One fine body&hellip;</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

然后,我想要得到的是在行中单击时显示模态,但在单击图标/跨度时收到警报。每次我点击图标时,模式都会出现。

最佳答案

试试这个。您将它绑定(bind)到每个不是 .noclick 的 td 并使用 .parent 获取数据目标。

$(".clickable td:not(.noclick)").click(function() {
  console.log("modal click");
  window.location = $(this).parent().data("target");
});
$(".clickableB").click(function() {
  alert('I got a click');
});
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<table class="table">
  <thead>
    <tr>
      <th>Employee</th>
      <th>Total hours</th>
      <th>Comments</th>
      <th>Options</th>
    </tr>
  </thead>
  <tbody>
    <tr data-toggle="modal" data-target="#events-modal" class="clickable success">
      <td>Pedro</td>
      <td>1</td>
      <td>This is a very loooooooooooooooooooong text</td>
      <td class="noclick">
        <span style="color:green" class="clickableB fa fa-check-square"></span>
        <span style="color:orange" class="clickableB fa fa-warning"></span>
        <span style="color:red" class="clickableB fa fa-minus-square"></span>
      </td>
    </tr>
  </tbody>
</table>

关于javascript - 在可点击的行内使 span 可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45738221/

相关文章:

javascript - Mongoose .js CastError : Cast to number failed for value "[object Object]" at path "undefined"

javascript - 如何更改 div 部分滚动条的 CSS 样式?

javascript - 如何正确使用 postMessage 与 html5 和现代浏览器进行跨域消息传递?我仍然收到错误

php - Firefox 中的 Ctrl+r 用于刷新页面和我的 PHP 代码

javascript - 选择 2 : tagging using an array from remote data?

javascript - ng-model 在选择输入中无法正常工作

javascript - Jquery-Visible 不工作

javascript - 隐藏 div 中的 tinyMCE 仅在第一次更新并且变为只读

javascript - 直接从 Javascript 渲染 JSF 组件

javascript - MVC3 站点脚本和 CSS 未加载