admin管理员组文章数量:1279117
I'm trying to use Bootstrap to make the rows clickable but all I found is about lists. Is any way to do this?
I wrote this:
<table class="table table-hover">
<tr>
<td><strong>FirstName</strong></td>
<td><strong>LastName</strong></td>
<td><strong>Start</strong></td>
<td><strong>End</strong></td>
</tr>
<tr><a href="user/student">
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
</a></tr>
</table>
but it didn't work.
I'm trying to use Bootstrap to make the rows clickable but all I found is about lists. Is any way to do this?
I wrote this:
<table class="table table-hover">
<tr>
<td><strong>FirstName</strong></td>
<td><strong>LastName</strong></td>
<td><strong>Start</strong></td>
<td><strong>End</strong></td>
</tr>
<tr><a href="user/student">
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
</a></tr>
</table>
but it didn't work.
Share Improve this question edited May 27, 2015 at 13:03 kguest 3,8543 gold badges31 silver badges32 bronze badges asked May 27, 2015 at 12:11 yehonatanyehonatan 2232 gold badges3 silver badges12 bronze badges 1- The question itself has nothing to do neither with js nor Bootstrap - it is a basic HTML question. – moonwave99 Commented May 27, 2015 at 12:18
6 Answers
Reset to default 3Why don't you simply attach an onclick
event like this:
<table class="table table-striped">
<tr onclick="window.location.href = 'url';">>
<td><strong>FirstName</strong></td>
<td><strong>LastName</strong></td>
<td><strong>Start</strong></td>
<td><strong>End</strong></td>
</tr>
<tr onclick="window.location.href = 'url';">
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
</a></tr>
</table>
Note: I would remend you to use jQuery as bootstrap also uses jQuery.
Don't put anchor in nested tr. It's not a proper use. Instead, use the
onclick="getElementById('edit-1').click()"
and add
style="cursor: pointer"
<table class="table table-hover">
<tr>
<td><strong>FirstName</strong></td>
<td><strong>LastName</strong></td>
<td><strong>Start</strong></td>
<td><strong>End</strong></td>
</tr>
<tr onclick="getElementById('edit-1').click()" style="cursor: pointer">
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td><a href="url-here" id="edit-1">dddd</a></td>
</tr>
Also you can add a function all tr same time;
$("tr").click(function(){
// your click time codes...
});
table-hover class only add to rows color change property. not click function definition!
So you want to be able to navigate to another page. If you are planning to go to another page as you said in the ments, you might want to use location.href = url. the first solution will work if you click on any row.
<table class="table table-hover">
<tr>
<td><strong>FirstName</strong></td>
<td><strong>LastName</strong></td>
<td><strong>Start</strong></td>
<td><strong>End</strong></td>
</tr>
<tr><a>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
</a></tr>
</table>
$("row").click(function(){
//to navigate to another page
location.href = 'user/student';//the other page's url
});
Another way to implement this is by using the onclick in the specific row. This solution will work if you click on the specific row that has the onclick.
<table class="table table-hover">
<tr onclick="location.href='user/student';">
<td><strong>FirstName</strong></td>
<td><strong>LastName</strong></td>
<td><strong>Start</strong></td>
<td><strong>End</strong></td>
</tr>
<tr><a>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
</a></tr>
</table>
Another way is to give an id to the row and use js or jquery to select that id and navigate to the page you want like below:
<table class="table table-hover">
<tr id="row1">
<td><strong>FirstName</strong></td>
<td><strong>LastName</strong></td>
<td><strong>Start</strong></td>
<td><strong>End</strong></td>
</tr>
<tr><a>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
</a></tr>
</table>
//now you can use this to navigate
$('#row1').on('click', function(){
window.location = "user/student";
});
I had the same problem.
I solved so:
1 - put ID in your table:
"tbListaFiadores"
2 - configure your table with :
clickToSelect: true,
3 - get click event :
$('#tbListaFiadores').on('click-row.bs.table', function (e, row, $element) {
console.log("onClickRow normal");
});
The cleanest way I see is to use jasny bootstrap ponent
Import the .js file into your project
Put on tbody:
<tbody data-link="row" class="rowlink hand-cursor">
Put an a element on first td element
<a href="#path"> path </a>
- Now the link is already working. To make it rich use this css file and its classes:
CSS
.hand-cursor {
cursor: pointer;
cursor: hand;
}
.table-hoverplus>tbody>tr:hover>td {
background-color: #eeeeee;
}
本文标签: javascriptHow to create clickable row in table using BootstrapStack Overflow
版权声明:本文标题:javascript - How to create clickable row in table using Bootstrap? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741302616a2371178.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论