更改DIV背景图像不起作用

时间:2012-09-23 18:41:19

标签: javascript css html

我有这样的功能:

function div_id( parent , child ) {
    var children = document.getElementById(parent).childNodes;
    for (var i=0; i < children.length; i++) {
        if (children[i].id == child ) {
            return children[i];
            break;
        }
    }
    return null;
}

我的div设置如下:

<div id="table_seats_box">
    <div id="table_seat_0">
        <div id="table_seat_0_d"></div>
        <div id="table_seat_0_player_name"></div>
        <div id="table_seat_0_portrait">
           <div id="table_seat_0_card_0"><div id="table_seat_0_card_1"></div></div>
           <div id="table_seat_0_chips"></div>
           <div id="table_seat_0_status"></div>
        </div>
    </div>
</div>

我的代码是这样的:

var t_s_p = "table_seat_" + player_table_position;
var t_s_p_p = "table_seat_" + player_table_position + "_portrait";
var children = div_id ( t_s_p , t_s_p_p );
children.style.backgroundImage = "url(images/class/" + image_id + ".png)";

但它似乎没有改变背景图像。难道我做错了什么? w,h和能见度在css上都很好。谢谢你的帮助。

编辑修复了“)”,但仍无效。

仅供参考:图片只是一个数字

1 个答案:

答案 0 :(得分:3)

您未关闭url(左括号,请将backgroundImage定义更改为:

children.style.backgroundImage = "url(images/class/" + image_id + ".png)";

注意:您可以选择简化div_id功能:

function div_id(parent, child) {
    return document.getElementById(parent).querySelector("#" + child); /*IE8+, all other modern browsers*/
}