[英]Changing star colour onClick - why doesn't this work?
I have a very simple star rating system. 我有一个非常简单的星级评分系统。 The issue I am having is with an onclick event that is supposed to change the colour of the star when clicked.
我遇到的问题是onclick事件,该事件应该在单击时更改星星的颜色。
I've created a jsFiddle for what I have so far. 到目前为止 ,我已经创建了一个jsFiddle 。
HTML HTML
<div class="rating">
<span id="five-stars" onclick="vote(5); return false;
document.getElementById('five-stars').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 5 stars out of 5">
☆</span><span id="fout-stars" onclick="vote(4); return false;
document.getElementById('four-stars').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 4 stars out of 5">
☆</span><span id="three-stars" onclick="vote(3); return false;
document.getElementById('three-stars').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 3 stars out of 5">
☆</span><span id="two-stars" onclick="vote(2); return false;
document.getElementById('two-stars').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 2 stars out of 5">
☆</span><span id="one-star" onclick="vote(1); return false;
document.getElementById('one-star').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 1 star out of 5">
☆</span>
</div>
CSS CSS
.rating {
color:#02dbdd;
font: 700 26px/normal 'Montserrat', sans-serif;
text-transform:uppercase;
display:block;
cursor:pointer;
margin-right:100px;
}
.rating {
unicode-bidi: bidi-override;
direction: rtl;
}
.rating > span {
display: inline-block;
position: relative;
width: 1.1em;
}
.rating > span:hover:before,
.rating > span:hover ~ span:before {
content: "\2605";
position: absolute;
}
Not sure why it won't change? 不知道为什么它不会改变?
(The RTL thing is a whole other problem to tackle... lol) (RTL是要解决的其他问题……大声笑)
You have three different problems that directly impact your test: 您有三个不同的问题直接影响您的测试:
$.post
it errors because $
is undeclared. $.post
会出错,因为未声明$
。 Add jQuery. return false
in the function before you try to set the colour. return false
。 Naturally the function returns before it reaches that line. return false
. return false
。 Fiddle: http://jsfiddle.net/nn5ytthL/5/ 小提琴: http : //jsfiddle.net/nn5ytthL/5/
<div class="rating">
<span id="five-stars" onclick="vote(5); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 5 stars out of 5">☆</span>
<span id="fout-stars" onclick="vote(4); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 4 stars out of 5">☆</span>
<span id="three-stars" onclick="vote(3); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 3 stars out of 5">☆</span>
<span id="two-stars" onclick="vote(2); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 2 stars out of 5">☆</span>
<span id="one-star" onclick="vote(1); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 1 star out of 5">☆</span>
</div>
JS stop execution when can't find vote(5) function. 找不到投票(5)函数时,JS停止执行。 Add it and remove
return
from the middle of onclick
handler then it works. 添加它并从
onclick
处理程序的中间删除return
,然后它可以工作。
function vote(){}
Here updated fiddle: http://jsfiddle.net/nn5ytthL/4/ 此处更新了小提琴: http : //jsfiddle.net/nn5ytthL/4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.