2012-05-26 3 views
0

많은 요소 (입력, 선택, 체크 박스 등)가있는 양식이 있습니다. 명시 적으로 요소별로 처리하지 않고 양식 안의 모든 요소에 동작을 적용하려면 어떻게해야합니까? [error] Threw an exception: document.myForm is undefined양식 안의 모든 요소를 ​​어떻게 요청할 수 있습니까?

내가이 일을 시도 : 그것은 제대로 작동

storeEval | 
window.document.getElementsByTagName('form')[0].id = 'myForm'; 
window.document.defaultView.getComputedStyle(window.document.getElementById('myForm')).getPropertyValue('background-color'); 

내가 오류가

storeEval | 
window.document.getElementsByTagName('form')[0].id = 'myForm'; 
window.document.defaultView.getComputedStyle(window.document.myForm.getElementsByTagName('*')).getPropertyValue('background-color'); 
| result 

:

여기 (셀레늄 IDE에서) 내 코드입니다.

나는이 작업을 수행하려고

: 나는 당신이 그렇게 할 이유를 잘 모르겠지만, 모든 아이를 참조하기 위해 [error] Threw an exception: document.myForm is undefined

+0

(getComputedStyle + getPropertyValue 메서드를 사용하여) 폼의 모든 요소에 대한 배경색을 가져와야합니다. – user1211063

+0

최상의 솔루션은 현재 수행중인 작업에 따라 달라지며 이는 분명하지 않습니다. – Simone

+0

코드에서'document.myForm'을 참조하고 있습니다.'myForm'에'HTMLFormElement' 객체를 저장 했으므로'myForm'이어야합니다. – Simone

답변

1

이 시도 :

Command: storeEval 
Target : myForm = selenium.browserbot.getCurrentWindow().document.getElementsByTagName("form")[0].childNodes; var colors = new Array(); for (i=0; i < myForm.length; i++) { colors[i] = window.document.defaultView.getComputedStyle(myForm[i]).getPropertyValue('background-color')}; colors.join() 
Value : result 

자바 스크립트 selenium.browserbot.getCurrentWindow()이 응용 프로그램의 창을 얻을 수 니펫을.

+0

큰 감사, 그것의 작품! – user1211063

0

:

var myForm = document.getElementsByTagName('form')[0]; 
var children = myForm.childNodes; 

오류가 발생합니다 당신이 거기 토륨하지 유일의 형태로 특정 요소를 참조하려는 경우

#my_form * { 
    color: #369 
} 

아직 : form 태그 내부 태그, 다음을 시도 할 수 있습니다

#my_form select, #my_form input, #my_form label, #my_form button { 
    color: #369 
} 

편집 : 당신은 그냥 행에서 그들을 지정할 수 있도록, 그들 중 많은에서. 당신은 자바 스크립트, 당신이, 당신은 단지 같은 선택기 사용할 수있다 (내가보기 엔 권 해드립니다한다)의 jQuery 프레임 워크를 사용을 통해 형태 안에 모든 요소를 ​​참조 할 경우 모든

$('#my_form *').css(...) 
$('#my_form select, #my_form input, #my_form label, #my_form button').css(...) 
+0

그는 자바 스크립트 솔루션을 원합니다 –

+0

감사합니다.하지만 JQuery 없이는 자바 스크립트가 필요합니다. ( – user1211063

1

먼저, 양식 태그에 id 속성을 설정해야 JavaScript로 쉽게 식별 할 수 있습니다.

는 양식 idmyForm 경우 예를 들어, 당신은 직접 후손이

var children = document.myForm.childNodes; 
// or 
var children = document.myForm.getElementsByTagName('*'); 

첫 번째 반환 목록을 할 수 모든 자손의 두 번째 반환하면서 목록을 표시합니다.

수정 : 양식을 id 이외의 다른 많은 방법으로 식별 할 수 있지만 어려울 수 있습니다. document.getElementsByTagName('form')으로 시도하십시오. 이것은 특히 페이지에 하나의 양식 만있는 경우에 효과적입니다.

+0

큰 thaks가 작동하지만 내 양식에는 ID가 없습니다. 유일한 작업 및 방법 ( – user1211063

+0

내 마지막 편집 확인 – Simone

+0

"window.document.getElementsByTagName ('form') [0] .id = 'forma'"로 내 양식에 ID를 부여 할 수 있습니까? 맞습니까? – user1211063

관련 문제