jquery-selectors

.keypress on a DIV tag?

我的梦境 提交于 2019-12-18 04:08:06
问题 Is there a way to get work .keypress on a div element like this?: <html> <body> <script type="text/javascript"> <!-- $('#idtext').keypress(function(event) { var keyCode = event.keyCode; $('#idtext').text(function(i, text) { return text + String.fromCharCode(keyCode); }); }); // --> </script> <div id="idtext"></div> </body> </html> 回答1: Yes: you need to add a tabindex attribute to the <div> to allow it to receive the focus. <div id="idtext" tabindex="1"></div> Also, the property you want for

Select numbers on a page with jQuery or Javascript

感情迁移 提交于 2019-12-18 04:07:30
问题 I'm just wondering if there's a way to locate numbers on a page with jQuery or plain Javascript. Here's what I want to do: Say "June 23" is on the page. What I want to do is be able to prepend and append some <span> selectors to the number. Using :contains() with jQuery selects the whole thing, not just the number. These strings are being generated without any wrapping elements by a Wordpress theme I'm working on, and I only want to select the number . Any help would be appreciated! Thanks

IE8 and jQuery selectors

两盒软妹~` 提交于 2019-12-18 03:33:49
问题 Today it came to my attention that a combination of jQuery selectors and the addClass() function does not work properly on IE8. For example, when I want to ensure that even-numbered rows are selected in a table, I wrote: jQuery(document).ready(function($){ $("#table1 tr:nth-child(even)").addClass("even"); } And for the CSS, I wrote: #table1 tr:nth-child(even), #table1 tr.even { background-color: #ff0; } In Firefox, Chrome, Safari and Opera, even without the pseudo-class selector in the CSS

Is there a way to combine $(this) with :nth-child?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 01:32:29
问题 I'm in the middle of an .each iteration and wanted to call out the 2nd or 3rd child for the each..but cant make it work. alert($(this + ' :nth-child(2)').attr('id')); My only option that I can think of is something terrible goofy like this: $(this).children(':first').next().attr('id', 'ddParam' + newCount); $(this).children(':first').next().next().attr('id', 'txt' + newCount); $(this).children(':first').next().next().next().attr('id'... 回答1: What you need is context. With context, the

Is there a way to combine $(this) with :nth-child?

点点圈 提交于 2019-12-18 01:32:18
问题 I'm in the middle of an .each iteration and wanted to call out the 2nd or 3rd child for the each..but cant make it work. alert($(this + ' :nth-child(2)').attr('id')); My only option that I can think of is something terrible goofy like this: $(this).children(':first').next().attr('id', 'ddParam' + newCount); $(this).children(':first').next().next().attr('id', 'txt' + newCount); $(this).children(':first').next().next().next().attr('id'... 回答1: What you need is context. With context, the

jQuery: Get HTML including the selector?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 23:33:27
问题 Say I have this HTML: <ul> <li id="example"><strong>Awesome</strong> example text</li> </ul> I want to be able to do something like $('#example').html() but right now doing that obviously only gets <strong>Awesome</strong> example text . So how can I get the HTML including the selected element? ie. <li id="example"><strong>Awesome</strong> example text</li> I'm using jQuery 1.4.4. 回答1: In this specific case: var outerHTML = $("<div />").append($('#example').clone()).html(); See this page for

jQuery: using a variable as a selector [duplicate]

纵饮孤独 提交于 2019-12-17 22:55:03
问题 This question already has answers here : jQuery selectors with variables (5 answers) Closed 6 years ago . I'm having problems using a variable as the selector for a paragraph on which I want to take action. Specifically I have several title elements and the same number of paragraphs. The desired result is that if I click on Title1 then I take an action on paragraph1. I made a simple case for development purposes whereby if I click on a title then the text of the corresponding paragraph

How can I refresh a stored and snapshotted jquery selector variable

谁说我不能喝 提交于 2019-12-17 22:19:44
问题 I ran yesterday in a problem with a jquery-selector I assigned to a variable and it's driving me mad. Here is a jsfiddle with testcase: assign the .elem to my obj var log both lengths to the console. Result => 4 Remove #3 from the DOM log obj to the console => the removed #3 is still there and the length is still 4. I figured out that jquery query is snapshotted? to the variable and can't?won't? be updated log .elem to the console.. yep Result => 3 and the #3 is gone Now I update .elem with a

Can you specify a “data-target” for Bootstrap which refers to a sibling DOM element without using an ID?

杀马特。学长 韩版系。学妹 提交于 2019-12-17 22:18:31
问题 I am dynamically adding Collapsable elements to a page. Bootstrap uses the "data-target" attribute to specify which element the collapse toggle applies to. From the docs: The data-target attribute accepts a css selector Is there a way to write a selector which specifies the next sibling of the parent element? All of the examples from the docs seem to use selections by ID. Specifically the HTML looks like: <div class="accordion-group"> <div class="accordion-heading"> <a class="accordion-toggle

Jquery element+class selector performance

爱⌒轻易说出口 提交于 2019-12-17 21:52:27
问题 I was hoping $('#childDiv2 .txtClass') or $('#childDiv2 input.txtClass') perform better when selecting <input type="text" id="txtID" class="txtClass"/> element. But according to this performance analysis $('.txtClass'); is the best selector among this. I'm using JQuery 1.7.2 Does anybody have explanation for this? HTML <div class="childDiv2"> <input type="text" id="txtID" class="txtClass"/> <p class="child">Blah Blah Blah</p> </div>​ JS $('.txtClass'); $('#childDiv2 .txtClass') $('#childDiv2