使用jQuery gt()和lt()访问无法正常工作的元素范围
我试图将使用jQuery gt()和lt()访问无法正常工作的元素范围,jquery,arrays,html-table,iteration,Jquery,Arrays,Html Table,Iteration,我试图将td元素的文本推送到一个数组中供以后使用。我在tr元素最后一列中的按钮上使用单击事件(我标记了一个IP地址) 我试图抓取每个大于0小于7的元素,但是,我的开发控制台显示最后一个元素仍然被抓取,这就是按钮 这是控制台输出: Array Val: xx.xxx.xx.xxx Array Val: AT&T Array Val: some random text 3 Array Val: <--- Notice the 3 here? That sh
td
元素的文本推送到一个数组中供以后使用。我在tr
元素最后一列中的按钮上使用单击事件(我标记了一个IP地址)
我试图抓取每个大于0小于7的元素,但是,我的开发控制台显示最后一个元素仍然被抓取,这就是按钮
这是控制台输出:
Array Val: xx.xxx.xx.xxx
Array Val: AT&T
Array Val: some random text
3 Array Val: <--- Notice the 3 here? That should the next 3 elements, which is fine
Array Val: <--- THIS SHOULD NOT BE HERE SINCE IT WASN'T SUPPOSED TO GET PUSHED TO THE ARRAY
更改我的lt()/gt()范围修复了它。我还添加了一些调试代码,以查看我推送到数组中的每个元素的html(outerHTML的部分):
$('bAcep')。在('click',function()上{
var text=$(this.parent().parent().parent().find('td:gt(0):lt(6)')//
//Click the button
$('#bAcep').on('click', function() {
//Get the parent of the parent of the parent of the button, find the 'td' elements
//between the first and the last (1 - 6).
var text = $(this).parent().parent().parent().find('td:gt(0):lt(7)');
//Now add each of those to the array
text.each(function() {
array.push($(this).text());
});
//Loop through array and print to console
$.each(array, function(index, val) {
console.log("Array Val: "+val);
})
})
$('#bAcep').on('click', function() {
var text = $(this).parent().parent().parent().find('td:gt(0):lt(6)'); //<-- Changed to 6 instead of 7
text.each(function() {
array.push($(this).text());
console.log("outerHTML: "+$(this).get(0).outerHTML); //<-- Added
});
$.each(array, function(index, val) {
console.log("Array Index: " + index + ", Array Val: "+val);
})
})