2017-11-20 2 views
0
<span id="continue" class="a-button a-button-span12 a-button-primary"><span class="a-button-inner"><input id="continue" tabindex="5" class="a-button-input" type="submit" aria-labelledby="continue-announce"><span id="continue-announce" class="a-button-text" aria-hidden="true"> 
      Continue 
     </span></span></span> 

내 스크립트를 사용하여 클릭하려는 '계속'버튼이있는 페이지의 일부분 위에있는 HTML 위."계속"버튼을 클릭하면 어떻게 자바 스크립트를 작성할 수 있습니까?

그래서 자바 스크립트로 작성,이 버튼을 클릭하려고합니다. 하지만 내가 시도한 것은 아무것도 없다.

내 시도 대답은 :

function() { 


     var goButton = document.getElementById("continue"); 
    goButton.click();}, 

왜 작동하지 않는 이유는 무엇입니까? 도와주세요, 제발 !

답변

1

스팬 및 입력 필드의 ID를 모두 "계속"으로 설정했습니다. ID는 단일 요소에 대해 고유해야합니다. 브라우저의 콘솔에 코드를 입력하면 다음을 반환합니다.

> var goButton = document.getElementById("continue"); 
< undefined 
> goButton.valueOf() 
< <span id="continue" class="a-button a-button-span12 a-button-primary"> 

입력 제출 버튼 대신 선택한 요소가 스팬임을 확인할 수 있습니다. 두 요소 중 하나의 이름을 변경하여 고유 한 ID를 갖고 스크립트에서 사용하도록해야합니다.

편집 : OP는 HTML이 변경 그래서 대신이 자바 스크립트를 사용할 수있는 고유하지 ID의 사용을 고정 할 수 없습니다 언급 : 나는 페이지의 HTML을 변경할 수 없습니다

function() { 
    var continueSpan = document.getElementById("continue"); 
    var goButton = continueSpan.firstElementChild.firstElementChild; 
    goButton.click();} 
+1

을 - 그 고쳐 졌어. –

+0

더 좋아지지는 않지만 selectChildElement 함수를 사용하여 스팬을 선택하고 입력 필드로 이동할 수 있습니다. 'var goButton = document.getElementById ("continue"); goButton.firstElementChild.firstElementChild.valueOf()' 출력 : ' ' – Tweek

+0

줄 바꿈이 어디에서 진행되는지 잘 모르겠습니다. 마지막에'goButton.click();}을 넣어 둡니까? –

관련 문제