jquery-selectors

jquery context selector vs .find()

一曲冷凌霜 提交于 2019-12-23 09:00:09
问题 What is more efficient? var container = $("#container"); // 1 var links1 = container.find("a"); // 2 var links2 = $("a", container); I personally prefer $("a", container) because it looks better, but are they different in performance? 回答1: The context selector $("a", container) is converted to find. find() will be faster but in most cases this could be ignored. I would go for find() as its syntax is quite stright forward for me. This post has performance comparison that would help you

Jquery: using two selectors for one .click event?

心不动则不痛 提交于 2019-12-23 07:49:59
问题 If I have something like this: $(".jq_elements").children("input").click(function() How do I add another selector to it, so that same event gets fired up when either one is clicked. Basically something like this would be needed: $(".jq_elements, .jq_another_div").children("input").click(function() Is something like this possible? edit: I did try this the way that I described above. Turns out my code was a bit crooked, but its fine now. 回答1: $("selector, selector") That very syntax works. Do

jQuery select with partial name

て烟熏妆下的殇ゞ 提交于 2019-12-23 06:57:48
问题 I'd like the count the element on the page with the name : MyElement_1 MyElement_2 Then I'd like get element MyElement Thanks, 回答1: var elements = $('[name^=MyElement]'); var length = elements.length; If you want some more advanced filtering, you can filter all the dom nodes by matching your own rules : var elements = $('[name]').filter(function(){ return /^MyElement_\d+$/.test($(this).attr('name')); }); 回答2: Use starts with selector: $('[name^="MyElement"]').length 回答3: better using $('input

how to iterate a result of jquery selector

扶醉桌前 提交于 2019-12-23 06:54:36
问题 I want to iterate a result of query selector. Html code <nav id="navigation"> <a href="#" tabindex="1" class="active_nav">nav1</a> <a href="#" tabindex="2">nav2</a> <a href="#"tabindex="3">nav3</a> </nav> when I use javascript alert($("#navigation >a")[0]); the result is the tag a href attribute I don't know why. 回答1: Use $.each $("#navigation > a").each(function() { console.log(this.href) }); $('#navigation > a')[0] ^ ^---- Selects the 1st dom object from the jQuery object | that is nothing

how to iterate a result of jquery selector

本小妞迷上赌 提交于 2019-12-23 06:54:27
问题 I want to iterate a result of query selector. Html code <nav id="navigation"> <a href="#" tabindex="1" class="active_nav">nav1</a> <a href="#" tabindex="2">nav2</a> <a href="#"tabindex="3">nav3</a> </nav> when I use javascript alert($("#navigation >a")[0]); the result is the tag a href attribute I don't know why. 回答1: Use $.each $("#navigation > a").each(function() { console.log(this.href) }); $('#navigation > a')[0] ^ ^---- Selects the 1st dom object from the jQuery object | that is nothing

How to get the total instance of the :contains() Selector

你离开我真会死。 提交于 2019-12-23 06:12:14
问题 I have these divs <div id="content" style="display:none">**Content1 goes here**</div> <div id="content" style="display:none">Content2 goes here</div> <div id="content" style="display:none">Content3 goes here</div> <div id="content" style="display:none">Content4 goes here</div> <div id="content" style="display:none">**Content1 goes here too**</div> <div id="content" style="display:none">**Content1 goes here again**</div> <div id="content" style="display:none">Content4 goes here part 2</div> I

jQuery get text

馋奶兔 提交于 2019-12-23 05:14:23
问题 I have a function that returns some html like this: return ("<span class='opt'>some text</span>"); I use it within the success callback function in $.ajax: $.ajax({ url: uri, cache: false, success: function(html){ $(tag).append(format(html)); } }); html give the <span></span> element. I want to retrieve just the text without the tags. I tried it using format(html).text() but it does not work. Any ideas? 回答1: If you want to do jQuery operations on a string of HTML, use jQuery to convert it

Select grand parent element attribute

◇◆丶佛笑我妖孽 提交于 2019-12-23 05:04:01
问题 I'm not so sharp on jquery but am setting up an analytics implementation (Using Google Tag MAnager) that uses titles. I have this line: var linkTitle = $(this).attr('title'); The this in question is a form checkbox and it has no title attribute. The grand parent li, however, does. I'd like to select the grand parent title attribute. Is that possible? The structure is like so: <ul> <li> <input> 回答1: you can try like this. $(this).parent().attr('title'); or you can also do like this. $(this)

Select grand parent element attribute

最后都变了- 提交于 2019-12-23 05:03:20
问题 I'm not so sharp on jquery but am setting up an analytics implementation (Using Google Tag MAnager) that uses titles. I have this line: var linkTitle = $(this).attr('title'); The this in question is a form checkbox and it has no title attribute. The grand parent li, however, does. I'd like to select the grand parent title attribute. Is that possible? The structure is like so: <ul> <li> <input> 回答1: you can try like this. $(this).parent().attr('title'); or you can also do like this. $(this)

How to write an integer in English words, in Javascript? [closed]

只谈情不闲聊 提交于 2019-12-23 04:58:11
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 months ago . Do we have any jQuery method that takes an int value and returns the value in words? 回答1: Working example here Here's a snippet that works from 1 to 999 that you might be able to modify to go higher, from http://snippets.dzone.com/posts/show/3710: var units = new Array ("Zero", "One", "Two", "Three", "Four",