1

这真的很简单。我正在使用 Asp.net 语言,我想使用 Javascript 创建一个标签。我在标签上配置的所有内容都是正确的,除非它的图像而且我不明白为什么,因为 javascript 无法识别代码上的任何类型的错误。我将在这里发布代码:

        var newlabel = document.createElement("label");
        contador=contador+1;//has no meaning here
        newlabel.setAttribute("id", "box" + someVar);
        newlabel.style.backgroundImage='url(Styles/PostitYellow.png)'; //doesnt load this image
        newlabel.setAttribute("Style","-moz-user-select: none;-khtml-user-select: none;");
        newlabel.setAttribute("onmousedown", "coordenadas(event,this.id)");
        newlabel.innerHTML = document.getElementById("MainContent_box").value;;
        document.getElementById("MainContent_revenuestreams").appendChild(newlabel);

我搜索了这是加载图像的正确方法,所以请告诉我我做错了什么

4

2 回答 2

3

You're overwriting style set by this line

newlabel.style.backgroundImage='url(Styles/PostitYellow.png)';

with this line

newlabel.setAttribute("Style","-moz-user-select: none;-khtml-user-select: none;");

swap them and it should work. Or better yet, combine all your style definitions in a single newlabel.setAttribute("Style" ... statement.

Or better, better yet - define your styles in a CSS class and simple assign that class to the label.

于 2013-09-12T16:04:03.593 回答
0

打开浏览器调试面板,查看 NETWORK 并查看在图像加载时是否出现 404 错误。此外,没有尺寸的空元素不会显示背景图像 - 所以给它一些内容或使用 CSS 调整大小。

于 2013-09-12T16:02:03.413 回答