jquery-selectors

Is this an even or odd element?

做~自己de王妃 提交于 2020-12-02 02:53:39
问题 So I saw this question a few moments ago on SO and it got me thinking. Basically the OP had something along these lines <div>a</div> <div>b</div> <div>c</div> <div>d</div> $('div').each( function() { //do something different based on whether even or odd div if ($(this) == ':even') {} //invalid markup I know! else {} }); Is there a way to tell inside the .each() whether your current element is an odd or even instance? There is the .filter method of jQuery, but it always returns true when it

Is this an even or odd element?

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-02 02:45:32
问题 So I saw this question a few moments ago on SO and it got me thinking. Basically the OP had something along these lines <div>a</div> <div>b</div> <div>c</div> <div>d</div> $('div').each( function() { //do something different based on whether even or odd div if ($(this) == ':even') {} //invalid markup I know! else {} }); Is there a way to tell inside the .each() whether your current element is an odd or even instance? There is the .filter method of jQuery, but it always returns true when it

How do I select the next “n” elements starting from the current element in jQuery?

不打扰是莪最后的温柔 提交于 2020-07-17 06:07:30
问题 How do I select the next "n" elements starting from the current element? What I mean is... $(this).attr(...); I want to do this "n" times. For the example of n=4: $(this).attr(...); $(this).next().attr(...); $(this).next().next().attr(...); $(this).next().next().next().attr(...); or perhaps do it in a loop: for (i = 0; i < n; i++) { $(this).next().attr(...); } How can I do this? Is there a way I can do this by selecting the next "n" elements or in a loop? 回答1: This should work: $(this)

Puppeteer - counting elements in the DOM

ε祈祈猫儿з 提交于 2020-05-28 12:01:12
问题 I know this has been answered here Puppeteer - counting elements by class name Yet, following this approach, I get 0 as my result using page.$$ - in my test, I always get 0 console.log((await page.$$('.clients-table > tbody > tr > td')).length); verified in browser using document.querySelectorAll() and the result is 4 How could this be??? 回答1: The data is not loaded yet. You can use waitForSelector to ensure the data is loaded properly. So just add this before counting the numbers, await page

Get the attribute value of each element from a jQuery set, into an array

生来就可爱ヽ(ⅴ<●) 提交于 2020-05-10 04:44:18
问题 How can I get all attributes (e.g. href ) of all elements matching a jQuery selector? 回答1: Something like this perhaps? var ids = []; $('.myClass').each(function () { ids.push($(this).attr('id')); // ids.push(this.id) would work as well. }); 回答2: Something like var idArray = $(".someClass").map(function(){ return this.id }).get().join(','); Working demo 回答3: One-liner with ES6 arrow functions, using jQuery .map(): const ids = $('a.someClass').map((i, el) => el.getAttribute('href')).get();

Get the attribute value of each element from a jQuery set, into an array

故事扮演 提交于 2020-05-10 04:44:02
问题 How can I get all attributes (e.g. href ) of all elements matching a jQuery selector? 回答1: Something like this perhaps? var ids = []; $('.myClass').each(function () { ids.push($(this).attr('id')); // ids.push(this.id) would work as well. }); 回答2: Something like var idArray = $(".someClass").map(function(){ return this.id }).get().join(','); Working demo 回答3: One-liner with ES6 arrow functions, using jQuery .map(): const ids = $('a.someClass').map((i, el) => el.getAttribute('href')).get();

Get the attribute value of each element from a jQuery set, into an array

青春壹個敷衍的年華 提交于 2020-05-10 04:43:28
问题 How can I get all attributes (e.g. href ) of all elements matching a jQuery selector? 回答1: Something like this perhaps? var ids = []; $('.myClass').each(function () { ids.push($(this).attr('id')); // ids.push(this.id) would work as well. }); 回答2: Something like var idArray = $(".someClass").map(function(){ return this.id }).get().join(','); Working demo 回答3: One-liner with ES6 arrow functions, using jQuery .map(): const ids = $('a.someClass').map((i, el) => el.getAttribute('href')).get();

Adding the ID or class into the exact match element

送分小仙女□ 提交于 2020-04-30 06:25:18
问题 Hi there I am expanding on my previous post: Jquery matching values. I was wondering if anyone knows how to have this also filter the ID or Class as it is doing with the data value. I am trying to build a filter like this in the end and have repeating elements show how many times they are repeated. I use TWIG for the HTML from Advanced Custom Fields Wordpress plugin. Eventually I want this to be a dropdown like: https://i.stack.imgur.com/I06cT.png you can refer to this post: Building a

JQuery: Get tag content excluding nested tags

断了今生、忘了曾经 提交于 2020-02-27 23:57:12
问题 I've got some HTML like the following: <span id="A">Text I'm interested in <span id="B">Other crap I don't care about</span> </span> I'm looking to get the text content of span "A" excluding any nested tags (i.e. the content of span "B" in the above example). I'm trying to get the text content, not the HTML content. Also, in my particular case, I know there will always be some text inside of span A prior to any other tags, but I'm interested in the less-constrained solution as well. The

JQuery: Get tag content excluding nested tags

痴心易碎 提交于 2020-02-27 23:49:13
问题 I've got some HTML like the following: <span id="A">Text I'm interested in <span id="B">Other crap I don't care about</span> </span> I'm looking to get the text content of span "A" excluding any nested tags (i.e. the content of span "B" in the above example). I'm trying to get the text content, not the HTML content. Also, in my particular case, I know there will always be some text inside of span A prior to any other tags, but I'm interested in the less-constrained solution as well. The