2012-11-10 61 views
3

无法指定PROTOTYPE脚本以在点击函数中添加或删除css类名称“selected”给img元素(已针对Jquery执行...),但它必须位于Prototype中。它让我疯狂。不能让它为原型工作....使用原型添加或删除类

我的原代码是(Magento的店)

<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; ?> 

jQuery的。

您可以转换原型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中拥有的东西,然后将其转换为原型。 – glortho

+0

添加到我的原始代码为Jquery的问题。想要原型,请。 – Divisum

+0

试试这个例子:http://www.tutorialspoint.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分,从eachinvoke感谢@Geek Num。为他+7分,还有1分给我。

+0

它的工作....非常感谢glortho。少一个问题。 thx – Divisum

+3

有'toggleClassName()'方法,它会为你添加/删除类名http://api.prototypejs.org/dom/Element/prototype/toggleClassName/ – Victor

+0

再次感谢victor和glotho – Divisum