我正在使用jQuery 1.4.3。
我有一些div中有微数据,我试图通过循环中的jQuery访问它们。 这些div的输出很像行和列。
<div data-row="1" data-col="1"> <div data-row="1" data-col="2"> <div data-row="1" data-col="3"> <div data-row="2" data-col="1"> <div data-row="2" data-col="2"> <div data-row="2" data-col="3"> <div data-row="3" data-col="1"> <div data-row="3" data-col="2"> <div data-row="3" data-col="3">我试图循环每一行,并获得该行中每个div的最大高度(虽然我没有完全用这个代码)。 使用此代码,我只想循环遍历有多少行。
这有效并将产生三个“1”警报:
// ACCESS THE HEIGHT OF EACH CELL $("[data-row='1']").each(function() { var R = $(this).attr("data-row"); alert(R); });我希望在运行时看到的是三个警报“1”,然后是三个警报“2”,然后是三个警报“3”。 代码“运行”时,我没有收到任何错误。 没有警报被抛出。 这不起作用:
$("[data-row>='1']").each(function() { var R = $(this).attr("data-row"); alert(R); });这段代码出了什么问题? 为什么找不到数据行值为1或更大的行?
I am using jQuery 1.4.3.
I have some divs that have microdata in them and I am trying to access them via jQuery in a loop. These divs will output much like rows and columns.
<div data-row="1" data-col="1"> <div data-row="1" data-col="2"> <div data-row="1" data-col="3"> <div data-row="2" data-col="1"> <div data-row="2" data-col="2"> <div data-row="2" data-col="3"> <div data-row="3" data-col="1"> <div data-row="3" data-col="2"> <div data-row="3" data-col="3">I am trying to loop through each row and get the maximum height of each div in that row (although I am not doing exactly that with this code). With this code, I just want to be able to loop through how ever many rows there are.
This works and will produce three alerts of "1":
// ACCESS THE HEIGHT OF EACH CELL $("[data-row='1']").each(function() { var R = $(this).attr("data-row"); alert(R); });What I expect to see when this runs is three alerts of "1", followed by three alerts of "2", followed by three alerts of "3". I do not get any errors when the code "runs". No alerts are thrown. This does NOT work:
$("[data-row>='1']").each(function() { var R = $(this).attr("data-row"); alert(R); });What's wrong with this piece of code? Why is it not finding any rows that have data-row value of 1 or greater?
最满意答案
HTML属性读取为字符串,而不是int。 此外, 根据jQuery文档 ,属性值应该在选择器中引用(因为它们是字符串)。
这就是为什么$("[data-row=1]")不起作用,而$("[data-row='1']")的确如此。
$("[data-row>='1']")不起作用,因为>=不是属性选择器。
要获取data-row >= 1 div,你将不得不使用filter ,并检查data-row的值(ps你可以做$("[data-row]")来获取具有该属性的所有div无论价值多少)。
$("[data-row]").filter(function(){ return parseInt($(this).data('row'), 10) >= 1; })注意:jQuery可以使用.data来获取data-*属性,而不是.attr 。
HTML attributes are read as strings, not as ints. Also, according to the jQuery docs, attribute values should be quoted in selectors (as they are strings).
That is why $("[data-row=1]") doesn't work, and $("[data-row='1']") does.
$("[data-row>='1']") doesn't work because >= is not an attribute selector.
To get divs with data-row >= 1, you're gonna have to use filter, and check the value of data-row (p.s. you can do $("[data-row]") to get all divs with that attribute regardless of value).
$("[data-row]").filter(function(){ return parseInt($(this).data('row'), 10) >= 1; })NOTE: jQuery can use .data to get data-* attributes, instead of .attr.
更多推荐
发布评论