【问题标题】:How to change Style background Property using javascript, With onmouseover?如何使用javascript更改样式背景属性,使用onmouseover?
【发布时间】:2015-04-08 18:50:19
【问题描述】:

如何使用 javascript 更改样式背景属性,使用 onmouseover ?

当鼠标悬停not rate 为什么id="all_rating" 不改变样式背景属性?

我该怎么做?

http://jsfiddle.net/1x8rLLpt/

<div onmouseover="show_star('-12')">not rate</div>
<br>
<div class="all_rating" id="all_rating">
</div>


<script>
function show_star(bg_position) {    
    document.getElementById("all_rating").style.background = "transparent url(http://image.ohozaa.com/i/eb0/OZDobo.png) no-repeat 0px +bg_position+px;";    
}
</script>

【问题讨论】:

标签: javascript html css


【解决方案1】:

这是因为您需要关闭字符串连接的引号。 JavaScript 不会解释字符串中的变量。您也不需要字符串末尾的分号,它会使 CSS 属性值无效。

document.getElementById("all_rating").style.background = "transparent url(http://image.ohozaa.com/i/eb0/OZDobo.png) no-repeat 0px " + bg_position + "px";

另外,我建议只更新 backgroundPosition 属性,而不是整个 background 属性。

document.getElementById("all_rating").style.backgroundPosition = "0px " + bg_position + "px"; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2016-03-02
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    相关资源
    最近更新 更多