elements

Access multiple elements of list knowing their index

耗尽温柔 提交于 2019-11-26 10:14:30
I need to choose some elements from the given list, knowing their index. Let say I would like to create a new list, which contains element with index 1, 2, 5, from given list [-2, 1, 5, 3, 8, 5, 6]. What I did is: a = [-2,1,5,3,8,5,6] b = [1,2,5] c = [ a[i] for i in b] Is there any better way to do it? something like c = a[b] ? You can use operator.itemgetter : from operator import itemgetter a = [-2, 1, 5, 3, 8, 5, 6] b = [1, 2, 5] print(itemgetter(*b)(a)) # Result: (1, 5, 5) Or you can use numpy : import numpy as np a = np.array([-2, 1, 5, 3, 8, 5, 6]) b = [1, 2, 5] print(list(a[b])) #

Best way to get child nodes

别等时光非礼了梦想. 提交于 2019-11-26 09:42:03
I was wondering, JavaScript offers a variety of methods to get the first child element from any element, but which is the best? By best, I mean: most cross-browser compatible, fastest, most comprehensive and predictable when it comes to behaviour. A list of methods/properties I use as aliases: var elem = document.getElementById('container'); var child = elem.children[0]; var child = elem.firstElementChild; // == children[0] This works for both cases: var child = elem.childNodes[0]; // or childNodes[1], see below That’s in case of forms, or <div> iteration. If I might encounter text elements:

Android ListView with onClick items

末鹿安然 提交于 2019-11-26 08:55:55
问题 I\'m a new programmer and new in Android. I\'m using this example http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and it works great. Now I want to make the items (Dell, Samsung Galaxy S3, etc) to call a function to open a new activity with different information each. For example: If I touch Dell, a new Activity has to show up showing me information about Dell. If I touch Samsung, the same thing. I Googled but couldn\'t find anything helpfull, any hint? I

Get multiple elements by Id

谁说胖子不能爱 提交于 2019-11-26 08:04:29
问题 I have a page with anchor tags throughout the body like this: <a id=\"test\" name=\"Name 1\"></a> <a id=\"test\" name=\"Name 2\"></a> <a id=\"test\" name=\"Name 3\"></a> The ID is always the same but the name changes. I need to populate a list of the names of these anchor tags, for example; Name 1, Name 2, Name 3. This is where I\'ve got to so far: document.write(document.getElementById(\"readme\").name); This writes out the name of the first anchor tag. I\'m in need of a way to get multiple

What&#39;s the best way to loop through a set of elements in JavaScript?

半世苍凉 提交于 2019-11-26 07:39:59
问题 In the past and with most my current projects I tend to use a for loop like this: var elements = document.getElementsByTagName(\'div\'); for (var i=0; i<elements.length; i++) { doSomething(elements[i]); } I\'ve heard that using a \"reverse while\" loop is quicker but I have no real way to confirm this: var elements = document.getElementsByTagName(\'div\'), length = elements.length; while(length--) { doSomething(elements[length]); } What is considered as best practice when it comes to looping

Is it possible to clone html element objects in JavaScript / JQuery?

随声附和 提交于 2019-11-26 04:44:12
问题 I am looking for some tips on how to solve my problem. I have a html element (like select box input field) in a table. Now I want to copy the object and generate a new one out of the copy, and that with JavaScript or jQuery. I think this should work somehow but I\'m a little bit clueless at the moment. Something like this (pseudo code): oldDdl = $(\"#ddl_1\").get(); newDdl = oldDdl; oldDdl.attr(\'id\', newId); oldDdl.html(); 回答1: Using your code you can do something like this in plain

Best way to get child nodes

一曲冷凌霜 提交于 2019-11-26 02:05:02
问题 I was wondering, JavaScript offers a variety of methods to get the first child element from any element, but which is the best? By best, I mean: most cross-browser compatible, fastest, most comprehensive and predictable when it comes to behaviour. A list of methods/properties I use as aliases: var elem = document.getElementById(\'container\'); var child = elem.children[0]; var child = elem.firstElementChild; // == children[0] This works for both cases: var child = elem.childNodes[0]; // or