如何从tr元素获取数据属性的值? [英] How can I get the value of a data attribute from my tr element?
本文介绍了如何从tr元素获取数据属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下内容:
<tr data-rk="050500" data-pk="0006000" id="row_2">
我正在尝试获取像这样的数据元素的值,但是它不起作用.我该怎么做 ?另外,如何获取该行的ID并将其放入变量中?
I am trying to get the value of the data elements like this but it does not work. How can I do it ? Also how can I get the id for the row and put that into a variable?
var data-rk = $(this).('data-rk');
请注意,$(this).当我使用它来设置CSS时,该部分已经起作用.我需要使用$(this).因为我想在单击时连续这样做.
Note that the $(this). part already worked when I used it to set CSS. I need to use $(this). as I want to do this for a row when it's clicked on.
推荐答案
除了使用.attr
方法,还可以使用.data
方法.
Beside using .attr
method, you could use .data
method.
var data_rk = $(this).data('rk');
更新:
.data
方法将根据您的情况尝试进行数字转换,所以
Update:
.data
method will try do number conversion in your case, so
var data_rk = $(this).data('rk'); // will give you number 6000
var data_rk = $(this).attr('data-rk'); // will give you string '0006000'
这篇关于如何从tr元素获取数据属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文