jquery-selectors

jQuery: See how many elements a selector matched?

99封情书 提交于 2019-12-22 01:24:19
问题 If I have a selector like $.('.active'); How can I see how many items that matched? Alternatively, is there an easy way to see if more than zero elements were matched? 回答1: call .length on the returned set. Do not use .size because: The .size() method is deprecated as of jQuery 1.8 回答2: How many: var count = $('.active').length; Check if it matched something: if ($('.active').length) // since 0 == false 回答3: You can use the native javascript length property: alert( $(".active").length ); You

jquery .not first or last

孤街醉人 提交于 2019-12-21 23:51:03
问题 $('a').not(':first').not(':last').css('z-index', '90'); do this. I'm having problem with this. any suggestion? 回答1: Not sure if it's exactly what you are after, but here we go... I've cleaned up the code a fair bit and removed the parts that I thought were not relevant to the question, mainlyis the jQuery cookie code. I also optimized some other parts like the previous/next tab selection and the hash manipulation The tab z-index magic was in the end reduced to a very fairly simple function:

Best Way to Get All DOM Elements with jQuery

吃可爱长大的小学妹 提交于 2019-12-21 19:56:18
问题 What's the best way to get all of the DOM elements on a page using jQuery? Thanks, DLiKS Edit: This is for use in a script that grayscales an entire page using grayscale.js - http://james.padolsey.com/demos/grayscale/. jQuery because I can! :P 回答1: var allOfThem = $('*'); You don't really need jQuery for this: var allOfThem = document.getElementsByTagName('*'); 回答2: document.getElementsByTagName("*") will return all DOM elements as "actual" elements, with all their contents and properties and

Does the jQuery .not selector work with .delegate?

梦想的初衷 提交于 2019-12-21 19:53:05
问题 I'm trying to make an Ajax call on all <a> elements inside the id #filter which don't have the class .noAjax and somehow i can't get it to work. could someone have a look at my syntax please? $("#filter").not($("a.noAjax")).delegate("ul#portfolio-list li a", "click", function() { if ($.browser.msie) { location.hash = "#/" + this.pathname; } else { location.hash = this.pathname; } return false; }); when i try to just use a or a.ajax (which i of course added before trying) as the selector for

jQuery “visible” doesn't work in all browsers, but in Firefox

痴心易碎 提交于 2019-12-21 17:52:51
问题 I've made a very simple fiddle here, and you can check it out in different browsers. It only works in Firefox. In other words, seems that $('#select-tag-id option:visible') doesn't work in other browsers. What's wrong? Is it a jQuery bug? The code is: <select id='items'> <option value='1' style='display: none;'>One</option> <option value='1' style='display: block;'>Two</option> <option value='1' style='display: block;'>Three</option> <option value='1' style='display: none;'>Four</option> <

Difference between E>F and E+F selectors?

谁说胖子不能爱 提交于 2019-12-21 17:39:43
问题 I read in jQuery that: E>F matches all elements with tag name F that are direct children of E . E+F matches all elements with tag name F that are immediately preceded by a sibling of tag name E . What is the difference between both? Can one provide a clear and concrete example? 回答1: First off, as jQuery borrows both > and + from CSS, they behave the same way as described in the Selectors spec; see child combinator and adjacent sibling combinator. "Children" refer to elements nested one level

jQuery select first element that has a certain class after this

雨燕双飞 提交于 2019-12-21 10:41:10
问题 Here is my fiddle: http://jsfiddle.net/jamesbrighton/wxWgG/4/ HTML: <div> <p class="click">Click 1</p> <p>This should be ignored</p> <p>This should be ignored</p> <p>This should be ignored</p> </div> <div> <p class="target">Target 1</p> </div> <div> <p class="target">Target 2</p> </div> <div> <p class="click">Click 2</p> <p>This should be ignored</p> <p>This should be ignored</p> <p>This should be ignored</p> </div> <div> <p class="target">Target 3</p> </div> <div> <p class="target">Target 4<

How to select by CSS styles in jQuery?

那年仲夏 提交于 2019-12-21 09:06:09
问题 I'm trying to find all the parent elements that have the CSS style display:none . I can't seem to get it to work though. Here's what I've got: var $parents = $(...).parents("[css=display:none]"); 回答1: If you want the ones that are actually display: none; (not just possibly contained in another display: none; ), you can use .filter() and .css() to get those parents, like this: var $parents = $(...).parents().filter(function() { return $(this).css('display') == 'none'; }); This gets the parents

jQuery Selector inside of variable?

我们两清 提交于 2019-12-21 09:03:24
问题 Hi I'm trying to get the IDs of all DIVs, inside another HTML File, with a specific Class. To load the file I use: $.get("blocks.html", function(data) { //here I don't know how :) }); Now what I'm looking for ist something like this: data.$('.block').each(... the rest is no problem so that I use the jQuery selectors not on my page code but instead inside the contend of the data variable. Thanks for help! 回答1: $.get("blocks.html", function(data) { var ids = $('<div/>').html(data).find('div

Is it possible to select $(this) AND use selectors in jQuery

…衆ロ難τιáo~ 提交于 2019-12-21 07:28:25
问题 I'm wondering if I can use $(this) as well as a class selector before running a function on them. So rather than doing; $(this).toggleClass('open'); $('.closed').toggleClass('open'); Do something more like; $(this, '.closed').toggleClass('open'); Whereas really, the above will select 'this' within the context of '.closed' Regards, 回答1: You can use add(): $(".closed").add(this).toggleClass("open"); It will add this element to the set of matched elements (i.e. .closed ). 来源: https:/