jQuery .each() 在 BB OS5 中出错?

发布于 2024-11-27 09:30:28 字数 1627 浏览 0 评论 0原文

我一直致力于在 BlackBerry OS5 设备(8530)上调试一些 jQuery。有很多问题,但我已经缩小了一个与 jQuery 的 .each() 有关的

逻辑:

$objectArray.each(function(){
   alert('test');
   if(...some logic...){
       $(this).addClass('testClass');
   };
})

在任何普通浏览器中,我都会收到警报,单击“确定”,然后看到如果逻辑语句为真,则该特定项目(在本例中为 TD)将获得更新的类。然后它会重复其余的项目,每个项目都会收到警报,我确定它,然后我会看到特定 TD 的类已更新。

然而,在 BlackBery 8530 上,我收到了每个警报,但 TD 并未一一更新。相反,它们在最后一个警报之后仅根据最后一个 TD 的 if 逻辑立即更新。

这个特定的浏览器很可能存在严重的 JS 问题,但我想知道是否有办法解决这个问题。在 jQuery 中是否有使用 .each() 的替代方法?

更新:

更详细的代码示例:

$TRs.each(function(){
    var $TR = $(this);
    var $checkBoxTD = $TR.find('td.td3');
    var $checkBox = $checkBoxTD.find('input');

    alert($checkBox.is(':checked'));

    if ($checkBox.is(':checked')!=true){
       $checkBoxTD.addClass('notSelected');
    }
});

我正在循环遍历表的每个 TR。每个 TR 内都有一个包含复选框的 TD (.td3)。我需要检查每一项。如果没有选中,我需要向 TD 添加一个类。

在良好的浏览器中,警报将显示 true 或 false,并且根据该特定警报,当您关闭警报时,您将看到相应的类应用到该行。然后对每一行重复。

在 BB OS5 的浏览器中,每个警报都会弹出正确的值,但类直到最后一个警报/循环之后才会更新,因此每个 TD 类仅使用最后一个循环的逻辑。

更新2(修复?):

感谢Alex,我做了更多的尝试,并找到了一种方法让它在顽固的浏览器中工作。

$TRs.each(function(idx){
    var $TR = $(this);
    var $checkBoxTD = $TR.find('td.td3');
    var $checkBox = $checkBoxTD.find('input');

    alert($checkBox.is(':checked'));

    if ($checkBox.is(':checked')!=true){
       $TRs.eq(idx).find('td.td3').addClass('notSelected'); // <-- the 'fix'
    }
});

不同之处在于,我将返回主 jQuery 对象 $TRs,并根据其索引专门从中获取其中一个元素。

因此,基于此,我的最后一个问题是:上述解决方案对于“好”浏览器有什么缺点吗?性能有影响吗?

I've been working on debugging some jQuery on a BlackBerry OS5 device (the 8530). There are a number of problems, but one that I have narrowed down is related to jQuery's .each()

The logic is as such:

$objectArray.each(function(){
   alert('test');
   if(...some logic...){
       $(this).addClass('testClass');
   };
})

In any normal browser I get the alert, click OK, and then see that particular item (in this case, a TD) get an updated class if the logic statement is true. It then repeats through the rest of the items, each getting an alert, me OKing it, and me seeing that particular TD's class get updated.

On the BlackBery 8530, however, I get each alert but the TDs aren't updated one-by-one. Instead, they all get updated at once after the last alert based on the if logic of the last TD only.

Odds are that there are serious JS issues with this particular browser, but I'm wondering if there is a way to get around this. Are there alternatives to using .each() in jQuery?

UPDATE:

A more detailed code example:

$TRs.each(function(){
    var $TR = $(this);
    var $checkBoxTD = $TR.find('td.td3');
    var $checkBox = $checkBoxTD.find('input');

    alert($checkBox.is(':checked'));

    if ($checkBox.is(':checked')!=true){
       $checkBoxTD.addClass('notSelected');
    }
});

I'm looping through each TR of a TABLE. Within each TR is a TD (.td3) that contains a checkbox. I need to check each one. If it's not checked, I need to add a class to the TD.

In good browsers, the alert will show a true or false, and, based on that particular alert, you will see the class being applied appropriately to that row as you dismiss the alert. It then repeats for every row.

In BB OS5's browser, each alert pops up with the proper value, but the classes aren't updated until AFTER the very last alert/loop, so every TD class then used the logic of the last loop only.

UPDATE 2 (fix?):

Thanks to Alex, I did some more playing with this and found a way to get this to work in the stubborn browser.

$TRs.each(function(idx){
    var $TR = $(this);
    var $checkBoxTD = $TR.find('td.td3');
    var $checkBox = $checkBoxTD.find('input');

    alert($checkBox.is(':checked'));

    if ($checkBox.is(':checked')!=true){
       $TRs.eq(idx).find('td.td3').addClass('notSelected'); // <-- the 'fix'
    }
});

The difference is that I'm going back to the main jQuery object $TRs and specifically grabbing one of the elements from it based on its index.

So based on that, my final question is: Does the above solution have any downsides for the 'good' browsers? Is there a performance hit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-12-04 09:30:28

试试这个:

$objectArray.each(function(idx, element){
   alert('test');
   if(...some logic...){
       $(element).addClass('testClass');
   };
})

更新:

试试这个,也许它的开销更少......

var $checkBox = $TRs.find('td.td3 input:not(:checked)');
$checkBox.each(function(){
    $(this).parent().addClass('notSelected');
});

Try this:

$objectArray.each(function(idx, element){
   alert('test');
   if(...some logic...){
       $(element).addClass('testClass');
   };
})

UPDATE:

Try this, maybe it has less overhead...

var $checkBox = $TRs.find('td.td3 input:not(:checked)');
$checkBox.each(function(){
    $(this).parent().addClass('notSelected');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文