[英]jQuery get path from data attribute
我有一个小问题,我想从数据属性获取“路径”并添加到后台。
HTML
<div>
<div data-image="../images/header.jpg"></div>
</div>
jQuery的
$('[data-image]').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)
});
如果还有更多元素,则可以使用each
元素
elm.each(function(){
$(this).css({
background: "url("+ $(this).data('image')+ ") no-repeat center",
backgroundSize: "cover",
height: ($(document).height()/ 3)
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.