我在使用javascript时遇到了一些问题:
http://www.fpmnky.com/test.php
光标不会变成指针,如果您单击行的文本区,它也不会转到代码google.com中的链接
<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
jQuery( function($) {
$('tbody tr[data-href]').addClass('clickable').click( function() {
window.location = $(this).attr('data-href');
}).find('a').hover( function() {
$(this).parents('tr').unbind('click');
}, function() {
$(this).parents('tr').click( function() {
window.location = $(this).attr('data-href');
});
});
$('tbody tr[data-href]').css( 'cursor', 'pointer' );
$('tbody tr[data-href]').hover(function() {
$(this).css('cursor','pointer');
});
});
</script>
<body>
<table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr data-href="http://google.com">
<td>text</td>
<td>text</td>
<td>text</td>
<td>text</td>
<td><a href="#">Edit</a> <a href="#">Delete</a></td>
</tr>
<tr class="even" data-href="http://google.com">
<td>text</td>
<td>text</td>
<td>text</td>
<td>text</td>
<td><a href="#">Edit</a> <a href="#">Delete</a></td>
</tr>
<tr data-href="http://google.com">
<td>text</td>
<td>text</td>
<td>text</td>
<td>text</td>
<td><a href="#">Edit</a> <a href="#">Delete</a></td>
</tr>
<tr class="even" data-href="http://google.com">
<td>text</td>
<td>text</td>
<td>text</td>
<td>text</td>
<td><a href="#">Edit</a> <a href="#">Delete</a></td>
</tr>
</tbody>
</table>
</body>
</html>不过,它似乎在jsfiddle上工作得很好:
我遗漏了什么?
发布于 2012-12-31 06:37:07
http:你的问题在这里
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>位置错误,如果你解决了这个问题,你的代码就可以工作了
顺便说一句,如果您愿意,您可以通过google链接到jquery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>发布于 2012-12-31 07:05:53
除了实际问题之外,我注意到当用户将鼠标悬停在实际链接上时,您正在删除td单击事件。您可以这样做的唯一原因是,单击气泡一直到tr。
因此,相反,绑定td (tr通常是不可点击的,它是您正在捕捉的td点击冒泡),如果它不是被点击的td,则返回。
你的代码
$('tbody tr[data-href]').addClass('clickable').click( function() {
window.location = $(this).attr('data-href');
}).find('a').hover( function() {
$(this).parents('tr').unbind('click');
}, function() {
$(this).parents('tr').click( function() {
window.location = $(this).attr('data-href');
});
});更少的代码
$('tbody tr[data-href] td').addClass('clickable').on('click', function(e) {
if (e.target!=this) return;
window.location = $(this).parent().data('href');
});此外,cursor:pointer不是在JS中添加CSS,而是只能在悬停时存在(因为指针在任何其他时间都不在元素上)
.clickable {
cursor:pointer;
}https://stackoverflow.com/questions/14095250
复制相似问题