javascript 使用 JQuery 设置 css 文件正文背景 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8252970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use JQuery to set css file body background url
提问by c12
I'm trying to change my body's background dynamically in a jQuery window resize event. The below is a simplified example that attempts to set the body background url property when the page loads, but its not the right syntax. Any ideas?
我正在尝试在 jQuery 窗口调整大小事件中动态更改我的身体背景。下面是一个简化的示例,它尝试在页面加载时设置正文背景 url 属性,但它的语法不正确。有任何想法吗?
test.css:
测试.css:
body {
margin: 0px auto;
font-family: HelveticaNeue;
}
test.html's JavaScript:
test.html 的 JavaScript:
$(document).ready(function() {
$("body").css("background", "url('image/setme.png')");
});
回答by Blender
I'd try being explicit about the property:
我会尝试明确说明该财产:
$("body").css("background-image", "url('image/setme.png')");
回答by Nighil
if no image is showing plz try this, access image from root directory
如果没有图像显示请试试这个,从根目录访问图像
$("body").css("background-image", "url('../image/setme.png')");
or
或者
$("body").css("background-image", "url('~/image/setme.png')");
回答by wizztjh
shouldn't it be
不应该是
$(window).resize(function() {
$("body").css("background", "url(image/setme.png)");
});
to trigger the resize event?
触发调整大小事件?