Tag: 每个

jQuery:找到重复的ID并删除除第一个之外的所有ID

$(‘[id]’).each(function () { var ids = $(‘[id=”‘ + this.id + ‘”]’); // remove duplicate IDs if (ids.length > 1 && ids[0] == this) $(‘#’ + this.id).remove(); }); 以上将删除第一个重复ID,但我想删除最后一个。 我试过$(‘#’+ this.id + ‘:last’)但无济于事。 小提琴 在附加动作发生时,应保留带有值’sample’的输入。

jQuery.each实现与本机Array.forEach不同

有没有人为什么是优秀的jQuery.each函数设计与(现在)原生Array.forEach ? F.ex: var arr = [‘abc’,’def’]; arr.forEach(function(entry, index) { console.log(entry); // abc / def }); 这绝对有道理。 但是jQuery选择将index作为第一个参数: $.each(arr, function(index, entry) { console.log(entry); }); 有谁知道这个设计决定背后的原因? 我总是广泛地使用$.each ,但它始终告诉我索引是第一个参数,因为它很少使用。 我知道jQuery通过this实现了直接引用,但是如果你这样做会非常困惑: ​var arr = [‘abc’,’def’]; $.each(arr, function() { console.log(this === ‘abc’); // false both times, since this is a String constructor });​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 并不是让我感到困扰,我更喜欢使用原生的polyfills来获得最常见的新数组函数,但我一直对设计决策感到好奇。 也许它是在浏览器实现原生forEach并且传统支持阻止他们更改它之前的较旧时间制作的,或者……? 或者,也许是这样设计的,因为它也可以在本机对象上使用,而不是把关键值放在回调中的“有意义”……? 旁注:我知道underscore.js(也许还有其他库)反过来(更类似于原生函数)。

jQuery’每个’循环使用JSON数组

我正在尝试使用jQuery的each循环来遍历此JSON并将其添加到名为#contentHere的div 。 JSON如下: { “justIn”: [ { “textId”: “123”, “text”: “Hello”, “textType”: “Greeting” }, { “textId”: “514”, “text”:”What’s up?”, “textType”: “Question” }, { “textId”: “122”, “text”:”Come over here”, “textType”: “Order” } ], “recent”: [ { “textId”: “1255”, “text”: “Hello”, “textType”: “Greeting” }, { “textId”: “6564”, “text”:”What’s up?”, “textType”: “Question” }, { “textId”: “0192”, “text”:”Come over […]

jQuery:如何使用从0以外的索引开始的每个

我有一个元素集合,我想使用每个元素循环,但我在一个外部for循环中循环它们。 当我在每个人中找到我想要的东西时,我会返回false来突破。 下一次外循环运行时,我想在我返回的元素之后的元素中开始。 一般代码示例: var nextIndex = 0; for (var j=1; j <= someCount; j++) { // do outside loop stuff $('#someElemID').find('.someClass').each(function(index) { if (/*this is right one*/) { // do something // next index should get passed to each function next loop… somehow? nextIndex = index + 1; return false; } }); } 我考虑过切换到for循环,但后来我对如何从.find(‘.someClass’)访问返回感到困惑。 […]

jQuery循环遍历data()对象

是否可以遍历data()对象? 假设这是我的代码: $(‘#mydiv’).data(‘bar’,’lorem’); $(‘#mydiv’).data(‘foo’,’ipsum’); $(‘#mydiv’).data(‘cam’,’dolores’); 我该如何循环呢? each()可以用于此吗?

如何用每个替换元素的attr href? // strip url

我试图用每种方法改变href, 这是演示,检查一下,你会看到没有变化 HTML: News Detail Sport Football​​​​​​​​​​​​ jQuery的: $(‘a’).each(function() { $(this).attr(‘href’).replace(‘#/’,”); //tried to erase #/ from all hrefs });​

链ajax并按顺序执行。 Jquery延期

我有3个需要ajax才能完成的进程。 但它是异步的,它无法做我想做的事情.. 让我们说: function a(param1, param2) { $.post(…, function(result){ if(result){ b(); } else { console.log(“failed a”); } }) } function b() { $.post(…, function(result){ if(result){ c(); } else { console.log(“failed b”); } }) } function c() { $.post(…, function(result){ if(result){ console.log(“successful”); } else { console.log(“failed b”); } }) } 我希望它像这样执行 a b c 正如你所看到的那样,该代码将完美地运行..但是如果使用循环。 var […]

每次完成jQuery时,触发函数

当.each完成循环我的元素时,如何启动一个函数,如重定向到一个新页面? 这是我目前的代码: $(‘#tabCurrentFriends > .dragFriend’).each(function(){ var friendId = $(this).data(‘rowid’); $.ajax({ type: “POST”, url: “../../page/newtab.php”, data: “action=new&tabname=” + tabname + “&bid=” + brugerid + “&fid=” + friendid, complete: function(data){ } }); });

在each()函数内部调用多个ajax ..然后在完成所有这些操作后执行某些操作?

让我稍微解释一下我的代码(对不起,如果有些错误,我刚刚从头开始编写这个例子,它与我现在的非常接近)。 HTML: Name 1: Email 1: Name 2: Email 2: Name 3: Email 3: Name 4: Email 4: JS: $(“#form”).submit(function(){ $(“.friendName[value!=”]”).each(function(){ var idEmail = ‘friendEmail’ + $(this).attr(“id”).replace(‘friendName’,”); if($(“#”+idEmail+”[value!=”]”).length > 0){ var name = $(this).val(); var email = $(“#”+idEmail).val(); // Submit the ajax request $.ajax({ type: ‘POST’, url: ‘ajax/url’, data: { name: name, email: email }, […]