jq 比较元素列表并找出奇数

发布于 2024-10-17 04:33:38 字数 403 浏览 5 评论 0原文

你好,有没有一种方法可以比较一些元素,

假设

<ul>
   <li>Cat</li>
   <li>Dog</li>
</ul>

我使用 ajax,它会返回

<ul>
   <li>Ant</li>
   <li>Cat</li>
   <li>Dog</li>
   <li>Fish</li>
</ul>

我需要闪烁 antfish 来表明它们是新的。

有什么想法吗?

hi is there a way to compare a few elements

lets say

<ul>
   <li>Cat</li>
   <li>Dog</li>
</ul>

if i use ajax it returns

<ul>
   <li>Ant</li>
   <li>Cat</li>
   <li>Dog</li>
   <li>Fish</li>
</ul>

i need to blink ant and fish to indicate they are new there.

any ideas ?

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

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

发布评论

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

评论(1

淤浪 2024-10-24 04:33:38

它不是很明显你想要什么,但也许是这样的(在现有的 ul 中查找 li 的匹配文本,并为找到的新 li 添加一个类“new”并将它们附加到现有的 ul 中?):

$currentUl = $('ul'); // this is the 'ul' with its li children u have on the page
//responseUl is the 'ul' string ur getting from the ajax call
$(responseUl).children('li').each( function (index, elem){
    if ( $currentUl.find(':[innerHTML=' + $(elem).text() + ']').length < 1 ){
        $currentUl.append( $(elem).addClass('new' ));
    }
});

Its not quite obvious what you want, but maybe something like this (looks for matching text of li in existing ul ands add a class 'new' for new li's found and append them to the existing ul?) :

$currentUl = $('ul'); // this is the 'ul' with its li children u have on the page
//responseUl is the 'ul' string ur getting from the ajax call
$(responseUl).children('li').each( function (index, elem){
    if ( $currentUl.find(':[innerHTML=' + $(elem).text() + ']').length < 1 ){
        $currentUl.append( $(elem).addClass('new' ));
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文