JavaScript可以设置background-size和background-position吗?

r00_a2lBUR 2017-03-26 05:52:08
var markerImg= document.createElement("img");
markerImg.style.background = 'url(http://ditu.amap.com/assets/img/poi-marker.png)';
//markerImg.style.background-size = '437px 267px'; //这里不注释掉会出错
markerImg.style.width = '25px';
markerImg.style.height = '34px';
//markerImg.style.background-position = '-9px -3px'; //这里不注释掉会出错
markerContent.appendChild(markerImg);

上面的那些代码,本来是想在地图上显示一个起点,但因为不能设置好background-size和background-position,无法正确显示,是不是JavaScript不可以设置background-size和background-position?
...全文
993 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
天际的海浪 2017-03-26
  • 打赏
  • 举报
回复
css属性名中的“-”在js中会被解释减法运算符。 这两个属性在js中要写成 backgroundSize backgroundPosition

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧