jquery-selectors

Jquery select all elements that have $jquery.data()

为君一笑 提交于 2019-12-18 10:14:35
问题 Select elements that i have previously set with jquery.data(); i.e. Select all elements with .data('myAttr') already been set. SOLUTION A jsfiddle to demostrate is Fiddle 回答1: You could do $('[data-myAttr!=""]'); this selects all elements which have an attribute data-myAttr which is not equal to '' (so it must have been set); you could also use filter() $('*').filter(function() { return $(this).data('myAttr') !== undefined; }); 回答2: The best and simple way to select them is: $('[data-myAttr]'

jquery selector: reference class inside of where i clicked:

╄→尐↘猪︶ㄣ 提交于 2019-12-18 09:21:48
问题 i have this html: <div class="title"><img class="arrow" src="rightarrow.gif" />Title</div> i have this click event: $(document).ready(function () { $('.title').live('click', function () { //NEED SOMETHING HERE TO CHANGE SOURCE $(".arrow").attr("src", "downarrow.gif"); }); }); as you can see i want to change the src attribute of the image. my selector above works, but there are other items on the page with class ="arrow", so i need a way to just select this one instance. 回答1: Use .find() to

jQuery Select Element that has no Class or ID

*爱你&永不变心* 提交于 2019-12-18 09:01:28
问题 Trying to find a selector to get all elements that have both no class, and no id set to them. So far I have 2 different outputs depending on if there is a space in the selector: // outputs var noID = $('*:not([id])');// 144 - may have a class var noClass = $('*:not([class])'); // 100 - may have an id var withSpace = $('*:not([id]) *:not([class])'); // 99 ? var noSpace= $('*:not([id])*:not([class])'); // 84 ? Which one is correct, my guess is the noSpace - but I don't know. Anyone tried this

Trying to select a 'body' tag from html that is returned by get() request

為{幸葍}努か 提交于 2019-12-18 08:50:36
问题 I'm making an ajax get call that returns me contents of html page. I'm trying to select contents of the body tag but my selector returns an empty jquery object. $.get(filename, function(data) { console.log($("body", data)); }) where data is contents of html file. 回答1: jQuery cannot select body of the response string, because the <body> tag disappears when the string is converted using $() . Hence, you have to select the body from the data string in another way, such as Regular expressions.

When do I need to escape metacharectars? (jQuery Selectors)

混江龙づ霸主 提交于 2019-12-18 08:34:27
问题 According to the jQuery docs, I need to escape metacharacters that occur in my selector strings, when they occur as a literal. However, I couldn't find very many specific examples of when and when not to escape selectors. So when and when don't I need to escape metacharacters, when they are to be interpreted as a literal, in: Attribute selectors? ie $("[attr=value]") Id selectors? ie $("#id") Class selectors? ie $(".class"); And, is there a way to write a function that replaces metachars in

When do I need to escape metacharectars? (jQuery Selectors)

浪子不回头ぞ 提交于 2019-12-18 08:33:51
问题 According to the jQuery docs, I need to escape metacharacters that occur in my selector strings, when they occur as a literal. However, I couldn't find very many specific examples of when and when not to escape selectors. So when and when don't I need to escape metacharacters, when they are to be interpreted as a literal, in: Attribute selectors? ie $("[attr=value]") Id selectors? ie $("#id") Class selectors? ie $(".class"); And, is there a way to write a function that replaces metachars in

Can URL tell jQuery to run a function?

喜你入骨 提交于 2019-12-18 06:56:25
问题 Have a question regarding URL and jQuery. Can I specify URL to tell jQuery to run a function? e.g http://www.website.com/about.html?XYZ to run a function XYZ(); ? 回答1: You can put code in that web page that examines the query parameters on the URL and then, based on what it finds, calls any javascript function you want. In your particular example, a simplified version would be like this: // code that runs when page is loaded: if (window.location.search == "?XYZ") { XYZ(); } or if you want it

Can jQuery create an element by a selector?

回眸只為那壹抹淺笑 提交于 2019-12-18 05:48:14
问题 Normally I would create an element in jQuery like this $('<div id="errors" class="red"></div>') Can I create an element given a selector using something like $.create("div#errors.red") ? Where that would return a jQuery object representing a div with the id of errors and the class of red ? 回答1: What you mean is Zen Coding like DOM creation. You can find the syntax of zen coding on the zen coding google project page. Usage would be: 1. download and add the zen-0.1.a.js file and add it to your

Getting a range of elements in one jQuery selector

穿精又带淫゛_ 提交于 2019-12-18 05:43:20
问题 My problem is that I have a table, but I only want a subset of the rows, from first index to last index. I thought you could do it like this: $('table tr:gt(2):lt(5)'); I thought it would give you only rows #3 and #4 but it ends up giving you more than that. How can I tell the selector to choose only rows #3 and #4? 回答1: You're pretty close, but the problem in your selector is the :lt(5) filter. You would want something like: $('table tr:gt(2):lt(2)'); The difference is that by the time the

.keypress on a DIV tag?

落爺英雄遲暮 提交于 2019-12-18 04:08:23
问题 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