我正在尝试将事件归因于 Bootstrap 表的不同列的单元格。
问题是:虽然我为每一列设置了一个类,但属于该类的事件不会被触发。我仍然尝试为单元格中添加的每个 div 设置一个类,但它的事件也没有被触发。
那么,如何使用 bootstrab 表获取 td 事件?
最佳答案
这是 bootstap 表的 td 点击事件的完整代码
$(document).ready(function(){
$('table td').click(function(){
alert($(this).html());
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<p>Click on the td below</p>
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f454047416f4a574e425f434a014c4042" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6805091a11280d10090518040d460b0705" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab0afb6a39abfa2bbb7aab6bff4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
关于javascript - 如何从 Bootstrap 表中获取td点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31617328/