1

我在项目下动态创建:

     var newItem = '<div title="' + elem.IP + '" id="' + elem.Alias + '-Status" class="elementStatus"><div class="image" id="' + elem.Alias + '-StatusImg" style="height:40px; background-image: ' + '@Url.Content("~/images/ongoing.gif")' + '"></div><div id="' + elem.Alias + '-StatusTxt" class="text">Waiting...</div></div>';

然后我通过执行动态设置 div 的背景图像:

$('#' + elem.Alias + '-StatusImg').css("background-image", '@Url.Content("~/images/ongoing.gif")');

但不显示 div (elem.Alias + '-StatusImg') 的背景图像。有任何想法吗?

4

1 回答 1

1

您需要url(x)在 CSS 规则中指定背景。尝试这个:

$('#' + elem.Alias + '-StatusImg').css("background-image", 'url("@Url.Content("~/images/ongoing.gif")")');

或这个:

 var newItem = '<div title="' + elem.IP + '" id="' + elem.Alias + '-Status" class="elementStatus"><div class="image" id="' + elem.Alias + '-StatusImg" style="height:40px; background-image: url(@Url.Content("~/images/ongoing.gif))"></div><div id="' + elem.Alias + '-StatusTxt" class="text">Waiting...</div></div>';

示例小提琴

于 2013-10-29T14:39:07.473 回答