簡體   English   中英

更改DIV背景圖片不起作用

[英]Changing DIV background image not working

我有一個像這樣的功能:

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上都很好。 謝謝您的幫助。

編輯修復了“)”,但仍然無法正常工作。

僅供參考:圖像只是一個數字

您沒有關閉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*/
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM