如何使用data-href打开新选项卡

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)

Mi-*_*ity 5

可以这样做:

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)