두 개의 다른 페이지에서 작동하는 북마크릿을 작성하려고합니다. 일단 요소를 잡으면 요소를 반복하고 처리 할 수 있지만 두 개의 다른 페이지를 수용하기 위해 두 개의 다른 클래스 이름으로 DIV 목록을 컴파일하려고합니다. 나는이 시작 :일반 Javascript에서 getElementsByClassName의 결과를 어떻게 연결합니까?
> traces=[].concat.apply(document.getElementsByClassName('fullStacktrace'),
document.getElementsByClassName('msg'))
[HTMLCollection[0], div#msg_2.msg.l1, ... div#msg_6460.msg.l1]
> traces[0]
[]
> traces[1]
<div class="msg l1" id="msg_2">…</div>
그래서 getElementsByClassName이 두 페이지 작동 다른 페이지에있는 반면
> traces=[].concat.apply(document.getElementsByClassName('fullStacktrace'),
document.getElementsByClassName('msg'))
[HTMLCollection[2]]
> traces[0]
[div.fullStacktrace, div.fullStacktrace]
> traces[1]
undefined
> traces[0][0]
<div class="fullStacktrace" style="height: auto;">…</div>
하지만 보이는 :
traces=
[].concat.apply(document.getElementsByClassName('fullStacktrace'),
document.getElementsByClassName('msg'))
하지만 첫 번째 페이지에이 결과
concat.apply는 첫 번째 인수를 반복하지 않지만 두 번째 인수는 반복합니다. 그래서 내가 두 번 CONCAT를 사용하고 괄호 모든 외출 시도 :traces=(
(
[].concat.apply(document.getElementsByClassName('fullStacktrace'))
)
.concat.apply(document.getElementsByClassName('msg'))
)
을하지만, 심지어 낯선 가져옵니다 첫 페이지는 말했다 :
Array[1]
> 0: HTMLCollection[0]
length: 0
> __proto__: HTMLCollection
length: 1
> __proto__: Array[0]
다른 :
Array[1]
> 0: HTMLCollection[283] // Lots of div#msg_3.msg.l1 sort of things in here
length: 1
>__proto__: Array[0]
을
div를 완벽하게 보완합니다.
두 그룹의 요소는 하나 또는 다른 페이지에만 있기 때문에 특정 경우에는 조건부를 사용할 수 있지만 위의 동작은 매우 놀랍기 때문에 이해하고 싶습니다. 어떤 아이디어? 참고로
, 이것은 CONCAT를 사용하려면 맥 크롬 버전 56.0.2924.87 (64 비트)
업 투표은'querySelectorAll' 솔루션을 제공하는 솔루션 제안
– Pineda
querySelectorAll에 대한 투표도 진행 중입니다. 그러나 괄호 안의 표현식이 [HTMLCollection [], HTMLCollection []]이되지 않은 이유와 첫 번째 표현식이 두 번째 HTMLCollection이 반복 된 이유가 무엇인지 이해할 수 없습니다 (첫 번째 경우에는 존재하지 않고 두 번째 경우에는 모든 div에서 추가됨), 첫 번째 HTMLCollection은 그대로 삽입되었습니다. –
@ android.weasel :'.concat()'은 실제 배열 인 경우에만 콜렉션을 결과로 평평하게하기 때문에'HTMLCollection'은 다른 값으로 간주됩니다. 첫 번째 예제에서'.apply()'를 사용하면'.msg' 요소가 추가되었으므로'msg' 요소가 있다면'traces [1]'은'undefined'가되어서는 안됩니다. 'msg'가 없다면 추가 할 것이 없으므로'undefined '가됩니다. –