javascript - 读取 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 也包括在内)
解决方案
)
除了您的代码按预期工作之外,您最后会出现语法错误。
<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>
推荐阅读
- visual-studio - Docker 容器在 Visual Studio 中工作,但在使用命令行时不工作
- sql - 如何在 SELECT 子句中使用 CASE 语句获取值?
- regex - 在“或”语句之后获取正则表达式以匹配完整的任意字符串,而不仅仅是最后一个字符
- python - 如何清理 Open3D 中的网格?
- mongodb - MongoDB 打印错误
- c# - 委托分配:在 JetBrains Rider 中捕获“this”引用
- linux - scripts/basic/fixdep:没有这样的文件或目录
- javascript - 使用 fetch 后如何重新加载页面
- java - 如何从 Java 应用程序调用打印机驱动程序的 .exe 文件
- node.js - env 数据输出正确但不被接受