使用 JQuery 在 HTML 中查找具有特定名称属性或 id 的元素
Find a element with particular name attribute or id in HTML with JQuery
我有一个包含几列的表,其中一列是链接,单击它时,我正在调用JQuery单击事件。单击时,我想获取另一列的值,该列是此列 (td) 的同级列。我怎么能得到它。
HTML
<table class="table table-bordered table-hover">
<thead>
<th>RID</th>
<th>Mobile Number</th>
<th>Outlet Name</th>
<th>Place</th>
<th>Email</th>
<th>Balance</th>
<th>Stock</th>
<th>Password</th>
</thead>
<tr>
<td data-name="rid">5111879</td>
<td data-name="mobile">9066587654</td>
<td data-name="outlet">Rachels</td>
<td>Indhiranagar, BL</td>
<td>rach@yahoo.com</td>
<td><i class="fa fa-inr"></i>378665</td>
<td><a href="#" id="retailer_stock">TRANSFER</a></td>
<td><a href="#" id="retailer_reset">RESET</a></td>
</tr>
</table>
单击 id=retailer_stock 时,会弹出一个模态,其值应为 data-name='rid'。我应该怎么做。
Javascript
$('#retailer_stock').click( function(event){
console.log($(this));
console.log($(event.target).closest('tr').children("[id = 'rid']"));
console.log($(this).closest('tr').children("[id = 'rid']"));
console.log($(event.target).closest('tr').find("[id = 'rid']"));
console.log($(event.target).siblings("[id = 'rid']"));
console.log($(this).closest("[id = 'rid']"));
})
在这些代码行中,最后两行不起作用,它没有为我获取结果,你们中的任何人可以解释为什么吗?
-谢谢
使用:
$('#retailer_stock').click( function(e){
alert( $(this).closest('tr').children().eq(0).text() )
})
检查演示:小提琴
如果要通过属性访问该单元格data-name
请使用:
$('#retailer_stock').click( function(e){
alert( $(this).closest('tr').find('[data-name="rid"]').text() )
})
但是,如果您计划在表中包含许多行,则最终将得到许多具有相同 retailer_stock
id 的元素。为避免这种情况,您可以决定将id="retailer_stock"
更改为class="retailer_stock"
,然后将选择器更改为$('.retailer_stock')
。
使用 jQuery 中的 Click 事件:
https://api.jquery.com/click/
捕获单击"retailer_stock",然后您可以从"rid"中检索HTML
http://api.jquery.com/html/
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$('#retailer_stock').click(function (e) {
alert($(this).closest('tr').find('td[data-name="rid"]').text())
})
});
</script>
</head>
<body>
<table class="table table-bordered table-hover">
<thead>
<th>RID</th>
<th>Mobile Number</th>
<th>Outlet Name</th>
<th>Place</th>
<th>Email</th>
<th>Balance</th>
<th>Stock</th>
<th>Password</th>
</thead>
<tr>
<td data-name="rid">5111879</td>
<td data-name="mobile">9066587654</td>
<td data-name="outlet">Rachels</td>
<td>Indhiranagar, BL</td>
<td>rach@yahoo.com</td>
<td><i class="fa fa-inr"></i>378665</td>
<td><a href="#" id="retailer_stock">TRANSFER</a></td>
<td><a href="#" id="retailer_reset">RESET</a></td>
</tr>
</table>
</body>
</html>
$('#retailer_stock').click(function(e){
$(this).parent().find('[data-name="rid"]').text()
});
相关文章:
- 获取一个对象´s属性的名称使用字符串
- 隔离作用域+绑定名称和仅在AngularJS中的隔离作用域之间的差异
- 如何检索上一个属性的名称属性
- 获取调用属于其属性或名称空间原型之一的函数的对象
- 如何使用 angularjs 中的表达式绑定属性名称
- AngularJS指令将名称属性绑定到模板元素
- 警告:失败的 propType:在“维度选取器”中未指定所需的属性“维度名称”.检查“连接(维度选择器)”的渲染方法
- 如何通过绑定名称加载类受限的 Angular 指令
- 如何使用 jQuery 获取自定义属性的名称
- 检查对象文本属性的名称
- "未捕获的类型错误:无法读取属性'名称'未定义的“;当触发一个功能时
- 从属性自身的函数中获取属性的名称
- 希望将属性的名称作为函数的参数传递.在Javascript
- 使用jQuery . CSS()调用中的JS变量来指定CSS属性的名称
- 根据元素id属性创建名称的对象
- 在Javascript中按值返回对象属性的名称
- 可以从另一个对象中引用属性的名称吗?
- 读取未知属性的名称
- AngularJS通过属性进行筛选,该属性的名称是从一个变量展开的
- Youtube使用“;默认“;作为对象属性的名称