3

cssクラス名「選択」をクリック機能に基づいてimg要素に追加または削除するためのPROTOTYPEスクリプトを理解できません(Jqueryではすでに行われています...)が、プロトタイプにある必要があります。そして、それは私を夢中にさせます。プロトタイプのためにそれを機能させることはできません....

私の元のコードは(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; ?>

Jクエリ用。

Prototype 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>
4

1 に答える 1

11

そのクリック ハンドラーのプロトタイプ バージョン (未テスト):

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

toggleClassName編集: @Victorの提案に従って変更されました。彼に+7ポイント、そして@Geek Numeachに感謝します。invoke彼は+7ポイント、私は残り1ポイント。

于 2012-11-11T00:12:43.260 に答える