我有一个小JavaScript函数,当单击具有此函数onclick的元素时会执行某些操作。
我的问题是: 我想在这个函数中为具有此函数onclick的HTML元素设置字体颜色,但我未能成功。我的代码:
<script type="text/javascript">
function selecteazaElement(id,stock){
document.addtobasket.idOfSelectedItem.value=id;
var number23=document.addtobasket.number;
number23.options.length=0;
if (stock>=6) stock=6;
for (i=1;i<=stock;i++){
//alert ('id: '+id+'; stock: '+stock);
number23.options[number23.options.length]=new Option(i, i);
}
}
</script>
我该如何使用它:
<li id = "product_types">
<a href="#" onclick='selecteazaElement(<?= $type->id; ?>,<?= $type->stock_2; ?>);'><?= $type->label; ?></a>
</li>
有什么建议吗?谢谢!
我添加了另一个函数(jquery函数),它部分地实现了我所需要的功能。新问题是:我只想将背景颜色设置在最后点击的项目上,而不是我点击的所有项目上。以上是代码:
$(document).ready(function() {
$('.product_types > li').click(function() {
$(this)
.css('background-color','#EE178C')
.siblings()
.css('background-color','#ffffff');
});
});
任何想法为什么?谢谢!