Jon*_*han 6 html javascript jquery
我正在制作一个可点击的表格行,但一旦点击我想打开一个新标签.我尝试使用data-target但是没有用.
<tr class="table-row" data-href="mypage.php" data-target="_blank"></tr>
<script type="text/javascript">
$(document).ready(function ($) {
$(".table-row").click(function () {
window.document.location = $(this).data("href");
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
可以这样做:
jQuery:JSFiddle 1
$('.table-row').each(function() {
var $th = $(this);
$th.on('click', function() {
window.open($th.attr('data-href'), $th.attr('data-target'));
});
});
Run Code Online (Sandbox Code Playgroud)
纯JS:JSFiddle 2
var tableRows = document.getElementsByClassName('table-row');
for (var i = 0, ln = tableRows.length; i < ln; i++) {
tableRows[i].addEventListener('click', function() {
window.open(this.getAttribute('data-href'), this.getAttribute('data-target'));
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3988 次 |
| 最近记录: |