简体   繁体   English

更改星形onClick的颜色-为什么不起作用?

[英]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">
&#9734;</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">
&#9734;</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">
&#9734;</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">
&#9734;</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">
&#9734;</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: 您有三个不同的问题直接影响您的测试:

  1. You've configured JS Fiddle so that the code in the JS pane is wrapped in a function and executed on load. 您已经配置了JS Fiddle,以便JS窗格中的代码被包装在一个函数中并在加载时执行。 This means that your function isn't a global and is inaccessible to the intrinsic event attributes. 这意味着您的函数不是全局函数,并且固有事件属性不可访问。 Change the configuration of JS Fiddle or rewrite your code to bind event handlers with JS instead of HTML. 更改JS Fiddle的配置或重写代码以将事件处理程序与JS(而不是HTML)绑定。
  2. You've not configured JS Fiddle to load jQuery, so when you try to call $.post it errors because $ is undeclared. 您尚未配置JS Fiddle来加载jQuery,因此当您尝试调用$.post会出错,因为未声明$ Add jQuery. 添加jQuery。
  3. You've put return false in the function before you try to set the colour. 在尝试设置颜色之前,已在函数中放入return false Naturally the function returns before it reaches that line. 自然,函数在到达该行之前返回。 Remove the return false . 删除return false
  1. vote function didn't exist. 投票功能不存在。
  2. You had return false so it didn't get to the changing of style. 您已返回false,因此无法改变样式。
  3. You can also reference the element with this. 您也可以以此引用元素。
  4. You were also missing the closing DIV tag even though this had nothing to do with your issue. 即使这与您的问题无关,您也缺少结尾的DIV标记。

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">&#9734;</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">&#9734;</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">&#9734;</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">&#9734;</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">&#9734;</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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM