jquery-selectors

How is the jQuery selector $('#foo a') evaluated?

谁说胖子不能爱 提交于 2019-12-17 21:44:07
问题 As a example of jQuery code (https://coderwall.com/p/7uchvg), I read that the expression $('#foo a'); behaves like this: Find every a in the page and then filter a inside #foo . And it does not look efficient. Is that correct? And if yes, how should we do that in a better way? 回答1: That is correct - Sizzle (jQuery's selector engine) behaves the same way as CSS selectors. CSS and Sizzle selectors are evaluated right-to-left, and so #foo a will find all a nodes, then filter those by nodes that

Where are scripts loaded after an ajax call?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 20:59:01
问题 suppose you have a simple web page that dynamically loads content that looks like this: - main.html - <!DOCTYPE html> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.6.2.js"> </script> <script type="text/javascript"> $(function() { $.ajax({ type: 'get', cache: false, url: '/svc.html', success: function(h) { $('#main').html(h); } }); }); </script> </head> <body> <div id='main'> loading... </div> </body> <

select special selector after $(this) selector

寵の児 提交于 2019-12-17 20:52:26
问题 for example I have this code <script> $(document).ready(function () { $('span').each(function () { $(this).html('<div></div>') ; if ( $(this).attr('id') == 'W0' ) { $( this > div ?!!! ).text('0') } if ( $(this).attr('id') == 'W1' ) { $( this > div ?!!! ).text('1') } if ( $(this).attr('id') == 'W2' ) { $( this > div ?!!! ).text('2') } }); }); </script> <span id="W0"></span> <span id="W1"></span> <span id="W2"></span> But $( this > div ) or $( this ' > div ' ) are wrong selector & doesn't work

How to find and replace text in CKEditor using Javascript?

房东的猫 提交于 2019-12-17 19:47:41
问题 How could I find and replace text in CKEditor using Javascript? Thanks for your suggestion! 回答1: try this editor = CKEDITOR.instances.fck; //fck is just my instance name you will need to replace that with yours var edata = editor.getData(); var replaced_text = edata.replace("idontwant", "iwant this instead"); // you could also use a regex in the replace editor.setData(replaced_text); you may want to put that in a blur event or soemthing 来源: https://stackoverflow.com/questions/5635118/how-to

jQuery selector, contains to equals

房东的猫 提交于 2019-12-17 18:52:19
问题 I have the folowing selector var likeComperssionOption = $('select[id*=ComparisionType]').eq(0) .find("option:contains('LIKE')"); this checks for an option which contains the word 'like' right? How do i find an option which is exactly with the word 'like'? Somthing like this: var likeComperssionOption = $('select[id*=ComparisionType]').eq(0) .find("option:equals('LIKE')"); 回答1: Just select all the options from the select and filter them by text() value: var likeComperssionOption = $('select

Cleanest way to get the next sibling in jQuery

╄→尐↘猪︶ㄣ 提交于 2019-12-17 18:34:02
问题 http://jsfiddle.net/mplungjan/H9Raz/ After quite some tests with next('a') and such, I finally found one that worked. I just wonder why next('a') did not, or closest or similar. Are there cleaner ways to get at the href of the link after the checkbox I click? $('form input:checkbox').click(function () { alert($(this).nextAll('a').attr("href")); }); <form> <div> <input type="checkbox" name="checkThis" value="http://www.google.com" />Check here<br/> <a href="http://www.google.com">click here</a

What's the meaning of the comma in a jQuery selector [duplicate]

痞子三分冷 提交于 2019-12-17 18:27:06
问题 This question already has an answer here : jQuery selector context (1 answer) Closed 3 years ago . <html> <head> <script type="text/javascript" src="jquery.js"></script> </head> <body> <ul> <li><strong>list</strong> item 1 - one strong tag</li> <li><strong>list</strong> item <strong>2</strong> - two <span>strong tags</span></li> <li>list item 3</li> <li>list item 4</li> <li>list item 5</li> <li>list item 6</li> </ul> <script type="text/javascript"> $('li').filter(function(index) { return $(

jQuery - selecting elements from inside a element

梦想的初衷 提交于 2019-12-17 17:31:57
问题 let's say I have a markup like this: <div id="foo"> ... <span id="moo"> ... </span> ... </div> and I want to select #moo. why $('#foo').find('span') works, but $('span', $('#foo')); doesn't ? 回答1: You can use any one these [starting from the fastest] $("#moo") > $("#foo #moo") > $("div#foo span#moo") > $("#foo span") > $("#foo > #moo") Take a look 回答2: Actually, $('#id', this); would select #id at any descendant level, not just the immediate child. Try this instead: $(this).children('#id');

Check if any ancestor has a class using jQuery

情到浓时终转凉″ 提交于 2019-12-17 17:25:23
问题 Is there any way in jQuery to check if any parent, grand-parent, great-grand-parent has a class. I have a markup structure that has left me doing this sort of thing in the code: $(elem).parent().parent().parent().parent().hasClass('left') However, for code readability i'd like to avoid this sort of thing. Is there any way to say "any parent/grandparent/great-grand-parent has this class"? I am using jQuery 1.7.2. 回答1: if ($elem.parents('.left').length) { } 回答2: There are many ways to filter

How to get the text value of a clicked link?

∥☆過路亽.° 提交于 2019-12-17 16:31:18
问题 I have matching text in different parts of a document. The first is a set of "tags" in a table like so: <div id="my-div"> <div><a href="#">tag 1</a></div> <div><a href="#">tag 2</a></div> </div> Then in several other parts of the document, I have a hidden element after the items I want to highlight when the matching link is selected like so: <div class="hide-me">tag 1</div> Then my click function is like this: $('#my-div a').click(function() { var txt = $(this).text(); console.log(txt); });