jquery-selectors

Find all elements in javascript that do not have a data- attribute

六月ゝ 毕业季﹏ 提交于 2020-02-02 11:19:29
问题 I need to find all elements in $('.post-content') that do not have data attributes. I tried to say something like if (!$('.post-content p').attr('data-example')) { ... } But obviously that does not work. So how would I do the following: Find all p tags that belong to .post-content who do NOT have a data-attribute and remove them. I think its very straight forward, but all the stack questions and docs, and jquery and the internet just has "how to add, how to find, how to add a value - to the

Find all elements in javascript that do not have a data- attribute

荒凉一梦 提交于 2020-02-02 11:18:45
问题 I need to find all elements in $('.post-content') that do not have data attributes. I tried to say something like if (!$('.post-content p').attr('data-example')) { ... } But obviously that does not work. So how would I do the following: Find all p tags that belong to .post-content who do NOT have a data-attribute and remove them. I think its very straight forward, but all the stack questions and docs, and jquery and the internet just has "how to add, how to find, how to add a value - to the

What is the difference direct descendent (>) vs. descendant in jQuery selectors?

南楼画角 提交于 2020-01-30 20:13:04
问题 What's the difference between these two jQuery statements? They seem to do the same thing by getting all the children div tags. $("#mainblock div") $("#mainblock > div") 回答1: $("#mainblock > div") = the childs only level $("#mainblock div") = all the childs+ desendents. 回答2: Have a look at jQuery Selectors Child Selector ("parent > child") - Hierarchy Selects all direct child elements specified by "child" of elements specified by "parent". Descendant Selector ("ancestor descendant")-

Find name of selected option using jQuery

半腔热情 提交于 2020-01-30 19:42:41
问题 I've made a jquery/ajax function that updates #courses, sending #fos's .val() and .text(), specifically of the one that is selected, like so: $('#selling #fos').change(function() { $.post('/ajax/courses', { fos_id: $('#selling #fos').val(), name: $('#selling #fos :selected').text() }, function(data) { $('#selling #courses').html(data); }); }); How do I extend this function so that it uses 'this', allowing me to reuse this function multiple times on the same page? I'm caught because you can't

How do you switch between two texts in one jQuery call?

痞子三分冷 提交于 2020-01-30 06:06:07
问题 Let's say you have a .click() call. What code could you write inside of that .click() call so that each time you click the selected element, you change the text between two strings. I'm assuming .toggle() and .text() would play a role here... 回答1: Try this: $('#someElement').toggle(function(){ $(this).text('text1'); }, function(){ $(this).text('text2'); } ); 回答2: Try something along these lines: $element.bind('click', function() { $(this).html($(this).html() == 'string1' ? 'string2' :

jQuery find and replace string

瘦欲@ 提交于 2020-01-26 15:13:51
问题 I have somewhere on website a specific text, let's say "lollypops", and I want to replace all the occurrences of this string with "marshmellows". The problem is that I don't know where exactly the text is. I know I could do something like: $(body).html($(body).html().replace('lollypops', 'marshmellows')); This would probably work, but I need to rewrite as little HTML as I can, so I'm thinking something like: search for the string find the closest parent element rewrite only the closest parent

Select the value of an attribute in jQuery

自作多情 提交于 2020-01-26 03:27:06
问题 I have the following table: <table summary='' id='table_csrdownloadcenter'> <thead> <tr> <th>text1</th> <th>text2</th> <th>text3</th> <th>text4</th> <th>text5</th> <th>text6</th> <th>text7</th> <th>text8</th> </tr> </thead> <tbody> <tr id='nom_du_pdf'> <td class='dc-date'></td> <td class='dc-dl'></td> <td class='dc-title'></td> <td class='dc-area'></td> <td class='dc-category'></td> <td class='dc-file'></td> <td class='dc-ranking'></td> <td class='dc-checkbox'><input type='checkbox' name='chk

What does $(''class for the same element', element) mean?

无人久伴 提交于 2020-01-25 09:01:27
问题 I don't unterstand this syntax: var dir = $("a.store").parents("table")[0]; var stores = $("a.store:has(b)", dir); What will store contain? What is the meaning of "$("a.store:has(b)", dir);" ? 回答1: It will return a collection of dom elements that match the css selector ("a.store:has(b)") that are children of the dom element stored in the 'dir' variable. 回答2: In your example, dir is the context of the selector. From the docs linked by Felix in his comment: By default, selectors perform their

Simple jQuery Toggle of children

偶尔善良 提交于 2020-01-25 01:55:37
问题 Um, why no worky? Trying to toggle a ul with this: $("#others").children(".btn").click(function(){ if ($(this).children("ul").is(":hidden")) { $(this).children("ul").slideDown(); } else { $(this).children("ul").slideUp(); } }); And: <div id="other"> <div id="galleries"> <a href="#" class="btn">Galleries >> </a> <ul id="select_gallery"> ... </ul> </div> <div id="events"> <a href="#" class="btn">Events >> </a> <ul id="select_event"> ... </ul> </div> </div> 回答1: The UL isn't a child of the .btn

jQuery text attribute selector

喜你入骨 提交于 2020-01-24 20:12:23
问题 I am trying to use the text attribute as a selector, but I can't quite grasp the behaviour, would really appreciate if someone could explain. For example given <span class="span_class"><a class="a_class">text</a></span> , $('a.a_class').text() gives "text" as expected. However $('a.a_class[text]') wouldn't match any elements, and neither would $('span.span_class > a.a_class[text]') , but $('span.span_class* > a.a_class[text]') would (although not in IE). The only workaround I can think of for