jquery-selectors

Find dynamic classname of element with jQuery

丶灬走出姿态 提交于 2019-12-21 06:09:12
问题 I'm having a unknown number of elements like so: <div class="item item-1"></div> <div class="item item-2"></div> <div class="item item-3"></div> What I want to do, is check if each item has a classname starting with "item-". If true, then extract the id. Something like this: $("container").each(function if ($(this).hasClassNameStartingWith("item-")) console.debug(theId); ); How is this possible? Thanks in advance! 回答1: Use the contains selector on the class attribute: $('container[class*="

jQuery attribute selector: [x=y] or [x=z]

做~自己de王妃 提交于 2019-12-21 04:06:51
问题 I have the following jQuery selector: $("a[href^='http://'],a[href^='https://']"); Is it possible to change this so that I don't need to specify a[href^= twice? For example, something like: $("a[href^='http://'||'https://']"); EDIT: My example of http and https should not be taken literally. I could be looking for values starting with y and z instead. 回答1: Quite simple if you're willing to use a second function call: $('a').filter('[href^="http://"],[href^="https://"]'); Or with tokens: var

Input vs :Input in jQuery

半城伤御伤魂 提交于 2019-12-21 03:47:21
问题 I wonder why people seems to prefer :input over input as a jQuery selector? Basically, this two lines seem to do the same thing : $('input:first').focus() $(':input:first').focus() But second version is more widely use, and I don't find why. Moreover, the :input selector seem slower according to this benchmark: http://jsperf.com/input-vs-input/2 回答1: :input is pseudo selector by jQuery which includes <buttons> , <textarea> , e.t.c input is a tag match which strictly matches <input> . This

Cheerio: How to select element by text content?

﹥>﹥吖頭↗ 提交于 2019-12-21 03:36:31
问题 I have some HTML like this: <span id="cod">Code:</span> <span>12345</span> <span>Category:</span> <span>faucets</span> I want to fetch the category name ("faucets"). This is my trial: var $ = cheerio.load(html.contents); var category = $('span[innerHTML="Category:"]').next().text(); But this doesn't work (the innerHTML modifier does not select anything). Any clue? 回答1: The reason your code isn't working is because [innerHTML] is an attribute selector, and innerHTML isn't an attribute on the

Use JQuery to select parent element of “this” (element clicked)

六眼飞鱼酱① 提交于 2019-12-21 03:30:35
问题 I have a jQuery script that creates a carousel to rotate images left and right on user click. At first, I wasnt planning on putting more than one carousel on a single page, but now the need has arrived. The problem is, I dont know how to refer to one carousel (the one clicked) when the user clicks a button. Heres the script $(function() { // Put last item before first item, in case user clicks left $('.carousel li:first').before($('.carousel li:last')); // Right click handler $('.right-button

Check if an select option exists based on text in jQuery 1.7

▼魔方 西西 提交于 2019-12-21 02:36:09
问题 So I have the following piece of HTML: <select id="sel"> <option value="0">Option1</option> <option value="1">Option2</option> <option value="2">Option3</option> <option value="3">Option4</option> </select> How do I check to see if #sel has an option based on the text value? ("Option1") 回答1: Try the following: var opt = 'Option1'; if ($('#sel option:contains('+ opt +')').length) { alert('This option exists') } Demo edit: The above snippet uses the jQuery contains selector which filters

What is the difference between jQuery's space and > selectors?

牧云@^-^@ 提交于 2019-12-20 19:10:36
问题 What's the difference between the space and > selectors? And possibly related, how can I look for something that's the direct child of something else, and not lower down the descendant line? 回答1: For: <ul> <li>Item 1</li> <li>Item 2 <ul> <li>Item 2.1</li> <li>Item 2.2</li> </ul> </li> <li>Item 3</li> </ul> For example $("ul > li").addClass("blah"); adds class "blah" to 1 2 and 3 whereas: $("ul li").addClass("blah"); add class "blah" to every list element. I'm not sure what you're referring to

Getting the 2nd child element

旧街凉风 提交于 2019-12-20 18:30:56
问题 I am still new in jquery and I have this code <div> abcsdf <div> first child</div> <div> second child</div> </div> I wanted to get the second child, they are dynamically populated using append and I don't know how to get it. I wanted to display $('the second element inner html here').dialog() etc.. Hoping someone can help me. Thanks 回答1: A number of ways to do this one. I'm going to assume the toplevel div has an id of 'top'. This is probably the best one: $('#top > :nth-child(2)').whatever()

How can I select a hidden field by value?

泪湿孤枕 提交于 2019-12-20 16:23:09
问题 I have the following HTML generated by an ASP.NET repeater: <table> <tr> <td><input type="hidden" name="ItemId" id="ItemId" value="3" /></td> <td>Terry</td> <td>Deleted</td> <td>Low</td> <td>Jun 21</td> </tr> <!-- rows repeat --> </table> How do I select a particular hidden field by value, so that I can then manipulate the columns next to it? 回答1: Using jQuery Selectors, you can target your element by a certain attribute matching the desired value: $('input[value="Whatever"]'); This way you

How to get the text of a div which is not a part of any other container in JQuery?

◇◆丶佛笑我妖孽 提交于 2019-12-20 12:03:37
问题 This should be real easy. Given below is the HTML. <div id='attachmentContainer'> #Attachment# <span id='spnAttachmentName' class='hidden'>#AttachmentName#</span> <span id='spnAttachmentPath' class='hidden'>#AttachmentPath#</span> </div> I want to get just the #Attachment# and not the other text. When I tried $("#attachmentContainer").text() it gives out all #Attachment#, #AttachmentName# as well as #AttachmentPath#. I know I could just put #Attachment# into another span and access it