2012-11-10 2 views
3

클릭 기능 (이미 Jquery ...에 대해 수행)을 기반으로 img 요소에 "selected"라는 CSS 클래스 이름을 추가하거나 제거하기위한 PROTOTYPE 스크립트를 그림하지만 프로토 타입에 있어야합니다. 그리고 그것은 나를 미치게합니다. 캔트프로토 타입이있는 클래스를 추가 또는 제거

내 원래 코드 (마 젠토 저장소) jQuery를 들어

<div class="block block-poll"> 
    <div class="block-title"> 
     <strong><span><?php echo $this->__('Community Poll') ?></span></strong> 
    </div> 
    <form id="pollForm" action="<?php echo $action ?>" method="post" onsubmit="return validatePollAnswerIsSelected();"> 
     <div class="block-content"> 
      <p class="block-subtitle"><?php echo $this->htmlEscape($poll->getPollTitle()); ?></p> 
      <?php if($poll_answers): ?> 
      <ul id="poll-answers"> 
       <?php foreach($poll_answers as $answer): ?> 
       <li> 
        <input type="radio" name="vote" style ="display:none;" class="radio poll_vote" id="vote_<?php echo $answer->getId() ?>" value="<?php echo $answer->getAnswerId() ?>" /> 
        <?php 
        $stripped = $answer->getAnswerTitle(); 
        $stripped_final = str_replace(" ", "_", strtolower($stripped)); //Value (simplified) 
        ?> 
        <span class="label"><label for="vote_<?php echo $answer->getId() ?>"><img src="http://www.passione.pt/media/poll/<?php echo $stripped_final; ?>.png" id="chooser" class="chooser" alt="<?php echo $this->htmlEscape($answer->getAnswerTitle()) ?>" onClick="document.getElementById('vote_<?php echo $answer->getId() ?>').checked =true;"/></label></span>   
       </li> 
       <?php endforeach; ?> 
      </ul> 
      <script type="text/javascript">decorateList('poll-answers');</script> 
      <?php endif; ?> 
      <div class="actions"> 
       <button type="submit" title="<?php echo $this->__('Vote') ?>" class="button"><span><span><?php echo $this->__('Vote') ?></span></span></button> 
      </div> 
     </div> 
    </form> 
</div> 
<?php endif; ?> 

입니다 ....이 프로토 타입을 위해 일합니다.

프로토 타입 1.7을 변형 할 수 있습니까? ...? (테스트되지 않은)이 클릭 핸들러의

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 
<script type="text/javascript"> 
    var Chooser = $('#poll-answers img'); 
    Chooser.click(function() { 
     $('.chooser').removeClass('selected'); 
     if(!Chooser.hasClass('selected')) { 
      $(this).addClass('selected');   
     } else { 
      $(this).removeClass('selected'); 
     } 
    }); 
</script> 
+0

jQuery에서 얻은 것을 보여주고 Protoype으로 변환합니다. – glortho

+0

질문에 Jquery의 원본 코드를 추가했습니다. 프로토 타입을 원해. – Divisum

+0

다음 예제를 시도해보십시오. http://www.tutorialspot.com/prototype/prototype_element_addclassname.htm – Alex

답변

9

프로토 타입 버전 :

$('poll-answers').on('click', 'img', function(event, element) { 
    $$('.chooser').invoke('removeClassName', 'selected'); 
    element.toggleClassName('selected'); 
}); 

편집 : 빅터의 제안 @ 따라 toggleClassName로 변경되었습니다. +7 점, @ Geek Num 덕분에 each에서 invoke으로 증가했습니다. 그에게 +7 점, 나를 위해 1 점 남았습니다.

+0

그것은 일했습니다 .... 많은 감사 glortho. 덜 한 가지 문제. thx – Divisum

+3

당신을 위해 클래스 이름을 추가/삭제할 수있는'toggleClassName()'메소드가 있습니다. http://api.prototypejs.org/dom/Element/prototype/toggleClassName/ – Victor

+0

이 라인'$$에 대해 victor와 glortho – Divisum

관련 문제