javascript - 如何用 JS 抓取索引?
问题描述
.tb button {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: gray;
border: 3px solid black;
margin: 1px;
}
body {
text-align: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div class="container">
<h1>Welcome to Connect Four!</h1>
<p>The object of this game is to connect four of your chips in a row!</p>
<p id="inp">:it is your turn, please pick a column to drop your blue chip.</p>
<table class="tb" align="center">
<thead>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</thead><br>
<thead>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</thead><br>
<thead>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</thead><br>
<thead>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</thead><br>
<thead>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</thead><br>
<thead>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</thead><br>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
我想使用 JQuery 创建返回单击按钮的索引的函数。除此之外,我想稍后将索引用于其他功能。例如,如果我单击 Google Chrome 上的索引号 0 按钮,我希望函数返回 0。如何使用 JavaScript 制作函数?我是初学者,因此简单的编码对我来说会很高兴。
解决方案
您可以$(this).parent('th').index()
用于获取列索引
对于行$(this).closest('tr').index()
索引
也应该使用tr
代替thead
标签
$("button").click(function() {
var col_index = $(this).parent('th').index();
var row_index = $(this).closest('tr').index();
alert(row_index + "-" + col_index);
});
$("button").click(function() {
var col_index = $(this).parent('th').index();
var row_index = $(this).closest('tr').index();
alert(row_index + "-" + col_index);
});
.tb button {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: gray;
border: 3px solid black;
margin: 1px;
}
body {
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div class="container">
<h1>Welcome to Connect Four!</h1>
<p>The object of this game is to connect four of your chips in a row!</p>
<p id="inp">:it is your turn, please pick a column to drop your blue chip.</p>
<table class="tb" align="center">
<tr>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</tr>
<tr>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</tr>
<tr>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</tr>
<tr>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
<th><button type="button" name="button"></button></th>
</tr>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
推荐阅读
- python - 找到类似元素时如何终止程序
- reactjs - 使用 Reactjs 的短信通知
- sql - 从表中查询以获取 JSON 结果
- javascript - Node + Express + Multer:上传一些元素为空的数组?
- vb.net - How to print a text file in Dot Matrix printer using chr values?
- python - 使用“If”语句检查 JSON 数据 webhook 响应
- javascript - 在 JavaScript Vanilla 中添加 2 个向量
- javascript - 创建函数来搜索整个表,而不仅仅是一列
- amazon-cognito - 有没有办法使用 Keycloak 联合 Cognito 用户池
- opengl - 双源混合与多渲染目标链接错误