Javascript jQuery从数据属性获取路径
我有一个小问题,我想从数据属性中获取“路径”并添加到背景中 HTMLJavascript 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()/
你知道怎么做吗?只需将元素缓存在变量中并使用它即可
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)
});
});