Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery从数据属性获取路径_Javascript_Jquery_Css_This_Jquery Data - Fatal编程技术网

Javascript jQuery从数据属性获取路径

Javascript jQuery从数据属性获取路径,javascript,jquery,css,this,jquery-data,Javascript,Jquery,Css,This,Jquery Data,我有一个小问题,我想从数据属性中获取“路径”并添加到背景中 HTML 你知道怎么做吗?只需将元素缓存在变量中并使用它即可 var elm = $('[data-image]'); // cache it elm.css({ background: "url("+ elm.data('image') +") no-repeat center", // use it backgroundSize: "cover", height: ($(document).height()/

我有一个小问题,我想从数据属性中获取“路径”并添加到背景中

HTML


你知道怎么做吗?

只需将元素缓存在变量中并使用它即可

var elm = $('[data-image]'); // cache it
elm.css({
    background: "url("+ elm.data('image') +") no-repeat center", // use it
    backgroundSize: "cover",
    height: ($(document).height()/ 3)
});
如果有更多元素,则可以使用
每个元素

elm.each(function(){
    $(this).css({
        background: "url("+ $(this).data('image')+ ") no-repeat center",
        backgroundSize: "cover",
        height: ($(document).height()/ 3)
    });
});

是否有多个元素具有该数据属性?如果是,请使用
每个

var elm = $('[data-image]'); // cache it
elm.css({
    background: "url("+ elm.data('image') +") no-repeat center", // use it
    backgroundSize: "cover",
    height: ($(document).height()/ 3)
});
elm.each(function(){
    $(this).css({
        background: "url("+ $(this).data('image')+ ") no-repeat center",
        backgroundSize: "cover",
        height: ($(document).height()/ 3)
    });
});