无法在PROTOTYPE脚本中添加或删除css类名称“selected”到基于click函数的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。
你可以转换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>
答案 0 :(得分:10)
该点击处理程序的原型版本(未经测试):
$('poll-answers').on('click', 'img', function(event, element) {
$$('.chooser').invoke('removeClassName', 'selected');
element.toggleClassName('selected');
});
修改:根据@ Victor的建议更改为toggleClassName
。感谢@Geek Num,他为+7分,从each
到invoke
。他得到+7分,剩下1分给我。