首页 > 解决方案 > 读取 Dom ID Jquery 方法

问题描述

$(document).on('click', '.table .table_head1 a', function(e){
    e.preventDefault();
    alert($(this).attr('id'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th class="table_head1">
                <a href="#" id="name">First Name
                </a>
            </th>
        </tr>
    </thead>
</table>

如果我单击 th/a,我想使用 id 的名称,但我编写的上述代码根本不起作用,它甚至没有检测到我点击了它。

(是的 Jquery 也包括在内)

标签: javascriptjquery

解决方案


)除了您的代码按预期工作之外,您最后会出现语法错误。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).on('click', '.table .table_head1 a', function(e){
   e.preventDefault();
   alert($(this).attr('id'));
});

</script>

<table class="table table-striped table-bordered">
    <thead>
      <tr>
        <th class="table_head1">
          <a href="#" id="name">First Name
          </a>
        </th>
      </tr>
     </thead>
</table>


推荐阅读