2012-06-26 3 views
1

아래 코드를 참조하십시오. 각 페이지마다 전체 버튼이 있습니다. 버튼을 누르면 페이지가 다음 섹션으로 진행되기를 원합니다. 섹션의 텍스트를 <h1></h1> 또는 <p></p> 태그로 묶은 경우를 제외하고 모두 정상적으로 작동했습니다. 예를 들어 섹션 1과 2를 참조하십시오.이 태그를 추가 한 후에 어떤 이유로 버튼이 작동하지 않습니다. 그것은 다른 섹션에서 일하고 있습니다. 1 절에서도 <h1></h1> 태그를 제거하면 정상적으로 작동합니다. 이 태그는 버튼의 기능과 어떤 관련이 있습니까?텍스트에 HTML 태그 추가 버튼 기능을 비활성화합니다.

<!DOCTYPE html> 
<html> 
<head> 
    <title>Selecting multiple DIV tags with jquery</title> 
    <script type="text/javascript" src="./jquery.js"> 
    </script> 
    <style type="text/css"> 

     html{ 
      overflow: hidden; 
     } 

     body { 
      backround-color: rgb(250, 250, 250); 
     } 

     h1 { 
      font-size: 48px; 
      color: rgb(90,90,90); 
     } 

     .slide { 
      display: block; 
      position: absolute; 
      border-style: solid; 
      border-width: 1px; 
      top: 0px; 
      left: 0px; 
      padding:20px; 
      height: 95%; 
      width: 100%; 
      font-family: Georgia; 
     } 
    </style> 
</head> 
<body> 

    <section class="slide"> 
     <h1>This is the first div.</h1> 
    </section> 

    <section class="slide"> 
     <p>This is the second div.</p> 
    </section> 


    <section class="slide">This is the third div.</section> 

    <section class="slide">This is the fourth div.</section> 

    <section class="slide">This is the fifth div.</section> 

    <section class="slide">This is the sixth div.</section> 



    <script type="text/javascript"> 

    // Assign ids to each section in the order they appear. 

    $(document).ready(function(){ 
     $("section").each(function(index){ 
      idtag = 's' + (index + 1) 
      $(this).attr('id', idtag); 
      $(this).append('<button>Next div</button>'); 
      $(this).css('opacity', 0); 
     }); 


    var presentation = function() { 

     $("section").each(function(index){ 
      $(this).css('opacity', 0); 
     });  
     // Check if the current url points to a specific id. If not point 
     // it to id = 1, otherwise point it to the id specified in the URL. 

     var currenturl = $(location).attr('href'); 


     var indexhash = currenturl.lastIndexOf('#') 

     if (indexhash === -1) { 
      var newurl = currenturl + '#s1'; 
      $("#1").css('opacity',1); 
      window.location.href = newurl; 
     } 
     else { 
      var currentid = currenturl.substring(indexhash, currenturl.length); 
      console.log(currentid); 
      $(currentid).css('opacity', 1); 
      window.location.href = currenturl; 
      // window.location.assign(currenturl); 
     } 

    }; 

     var nextdiv = function() { 
      currenturl = location.href; 
      currentid = location.hash; 
      console.log(currentid); 
      newidnum = parseInt(currentid.substring(currentid.indexOf('s')+1, currentid.length),10)+1; 
      newid = "#s" + newidnum; 
      newurl = currenturl.substring(0,currenturl.lastIndexOf('#'))+newid; 
      console.log(newurl); 
      window.location.href = newurl; 

     }; 

     // $(document).ready(presentation); 
     presentation(); 
     $(window).bind('hashchange', presentation); 
     $('button').bind('click', nextdiv); 

    }); 

    </script> 
</body> 
</html> 
+0

왜 div가 아닌 Sections를 사용하고 있습니까? – Fallenreaper

+0

전체 스크립트가'presentation (프리젠 테이션) '이 아닌'$ (document) .ready (...)'안에 있어야합니다. – Mathletics

+0

그래, 나는 $ (함수() {...})의 jquery 형태로 아무것도 보지 않는다; – Fallenreaper

답변

1

이이 섹션의 요소 내에서 HTML 요소를 함께 할 수 없다, 당신이 서로의 상단에 모든 요소를 ​​적재 한 사실과는 실제로 제로로 불투명도를 설정하는 대신합니다 디스플레이 속성 (display:none vs opacity:0)으로 숨 깁니다. 잘 동작하는 첫 번째 섹션 요소를 제외하고 모두 제거하면이 부분을 볼 수 있습니다. 또한 Chrome의 개발자 도구와 같은 도구를 사용하여 코드를 검사하면 여섯 번째 섹션 요소가 실제로 최상위 섹션 요소이며 그 아래의 모든 요소를 ​​차단한다는 것을 알 수 있습니다. 또한 섹션 요소의 하위 버튼이 아닌 항상 동일한 맨 위 버튼을 클릭하고 있습니다. 각 버튼에 ID를 할당하고 콘솔에 로그인하여 볼 ​​수 있습니다. $('button').click(function(){console.log($('button').attr('id'));});

+0

정말 고마워요. 불투명도에 대한 더 나은 대안이 있는지 묻고 싶었습니다. 그리고 다른 것들과 요소가 겹치는 문제가있을 수 있다는 생각이 들었습니다. 비록 내가 여기에 게시 된 문제는 그와 관련이 없다는 것이 아닙니다. html 태그 요소를 추가 한 후에 만 ​​문제가 발생한다는 것은 이상한 일입니다. 귀하의 답변에 감사드립니다. 지금은 잘 작동하고 있습니다. – Curious2learn