jquery-selectors

Find all elements based on ids using regex on jQuery selector

社会主义新天地 提交于 2019-12-17 09:31:06
问题 I've got several elements with unique ids like so: <div id='item-1-top'></div> <div id='item-2-top'></div> <div id='item-3-top'></div> I was hoping that the following would work using jQuery: $("#item-[.]+-top").each(function() { $(this).hide(); }); I do not have a good grasp of regular expressions and I would appreciate some input, as the above appears to be incorrect. 回答1: If you were doing this with regex, the expression would simply be: item-\d-top Where the \d indicates any single digit

Multiple selector chaining in jQuery?

 ̄綄美尐妖づ 提交于 2019-12-17 08:56:15
问题 Normally when I use a class as a selector I try to use an "id" selector with it so it does not search through the entire page but only an area where the class would be. However I have a partial view with code in it. This partial view (common code) gets wrapped around a form tag. I have: <form id="Create"> // load common code in from partial view </form> <form id="Edit"> // load common code in from partial view </form> Now in this common code I need to attach a plugin to multiple fields so I

Multiple selector chaining in jQuery?

烈酒焚心 提交于 2019-12-17 08:55:17
问题 Normally when I use a class as a selector I try to use an "id" selector with it so it does not search through the entire page but only an area where the class would be. However I have a partial view with code in it. This partial view (common code) gets wrapped around a form tag. I have: <form id="Create"> // load common code in from partial view </form> <form id="Edit"> // load common code in from partial view </form> Now in this common code I need to attach a plugin to multiple fields so I

jQuery: select all elements of a given class, except for a particular Id

倾然丶 夕夏残阳落幕 提交于 2019-12-17 08:16:06
问题 This is probably pretty simple. I want to select all elements of a given class thisClass , except where the id is thisId . i.e. something equivalent to (where -/minus implies remove): $(".thisClass"-"#thisId").doAction(); 回答1: Use the :not selector. $(".thisclass:not(#thisid)").doAction(); If you have multiple ids or selectors just use the comma delimiter, in addition: (".thisclass:not(#thisid,#thatid)").doAction(); 回答2: Or take the .not() method https://api.jquery.com/not/ $(".thisClass")

:nth-of-type() in jQuery / Sizzle?

久未见 提交于 2019-12-17 07:30:21
问题 It surprised me that Sizzle (the selector engine jQuery uses) comes with a built-in :nth-child() selector, but lacks an :nth-of-type() selector. To illustrate the difference between :nth-child() and :nth-of-type() and to illustrate the problem, consider the following HTML document: <!doctype html> <html> <head> <meta charset="utf-8"> <title>:nth-of-type() in Sizzle/jQuery?</title> <style> body p:nth-of-type(2n) { background: red; } </style> </head> <body> <p>The following CSS is applied to

jQuery :contains(), but to match an exact string

假装没事ソ 提交于 2019-12-17 07:28:43
问题 As said in the title, I'd like to find something like :contains() but to match an exact string. In other words, it shouldn't be a partial match. For example in this case: <div id="id"> <p>John</p> <p>Johny</p> </div> $("#id:contains('John')") will match both John and Johny , while I'd like to match only John . Thanks in advance. EDIT: ES2015 one-liner solution, in case anyone looked for it: const nodes = [...document.querySelectorAll('#id > *')].filter(node => node.textContent === 'John');

A cleaner way to select by multiple possible attribute values?

你离开我真会死。 提交于 2019-12-17 06:51:50
问题 Is there a possibility in jQuery to select by multiple possible attribute values without having to use a comma separated list of selectors. So in stead of: #list1 > option[value="1"], #list1 > option[value="2"], etc Something like: #list1 > option[value="1"|value="2"], etc 回答1: Not that I know of. The cleanest way I can think of doing this is to first select using the common elements across all items, then just .find() or .filter() the OR values out. Something like $('#list1 > option[value]')

jQuery hasClass() - check for more than one class

二次信任 提交于 2019-12-17 04:44:17
问题 With: if(element.hasClass("class")) I can check for one class, but is there an easy way to check whether "element" has any of many classes? I am using: if(element.hasClass("class") || element.hasClass("class") ... ) Which isn't too bad, but I am thinking of something like: if(element.hasClass("class", "class2") Which unfortunately doesn't work. Is there something like that? 回答1: How about: element.is('.class1, .class2') 回答2: element.is('.class1, .class2') works, but it's 35% slower than

How do you check if a selector matches something in jQuery? [duplicate]

邮差的信 提交于 2019-12-17 03:22:19
问题 This question already has answers here : Is there an “exists” function for jQuery? (42 answers) Closed 3 years ago . In Mootools, I'd just run if ($('target')) { ... } . Does if ($('#target')) { ... } in jQuery work the same way? 回答1: As the other commenters are suggesting the most efficient way to do it seems to be: if ($(selector).length ) { // Do something } If you absolutely must have an exists() function - which will be slower- you can do: jQuery.fn.exists = function(){return this.length

How do you check if a selector matches something in jQuery? [duplicate]

蓝咒 提交于 2019-12-17 03:22:13
问题 This question already has answers here : Is there an “exists” function for jQuery? (42 answers) Closed 3 years ago . In Mootools, I'd just run if ($('target')) { ... } . Does if ($('#target')) { ... } in jQuery work the same way? 回答1: As the other commenters are suggesting the most efficient way to do it seems to be: if ($(selector).length ) { // Do something } If you absolutely must have an exists() function - which will be slower- you can do: jQuery.fn.exists = function(){return this.length