javascript - HTML/JS : Setting "100% auto" for background-size property truncates to "100%" but "auto 100%" does not

标签 javascript html css

对于 div,为什么将背景大小属性设置为“100% auto”会被 chop 为“100%”,而“auto 100%”却不会?

目标是允许 background-size 属性为“100% auto”。

“100% 自动”不同于“100%”,不是吗?

代码笔:https://codepen.io/anon/pen/xMLXKz?editors=1111

<div id="test1"></div>
<div id="test2"></div>

div {
  width: 200px;
  height: 200px;
  background: blue;
}

$("#test1")[0].style["background-size"] = "100% auto";
$("#test2")[0].style["background-size"] = "auto 100%";

console.log($("#test1")[0].style["background-size"]);
console.log($("#test2")[0].style["background-size"]);

console.log("Done")

这个输出:

"100%"
"auto 100%"
"Done"

最佳答案

如您所见here at MDN - background-size , 它清楚地表明当第二个值(即 height )不存在时,浏览器将视为 auto

所以,我认为可以检查 $("#test1")[0].style["background-size"]< 中的 /\s/ 字符 值检查空格,如果不存在(RegExp 返回 -1 )将 auto 附加到输出以变为 100%自动

关于javascript - HTML/JS : Setting "100% auto" for background-size property truncates to "100%" but "auto 100%" does not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528451/

相关文章:

javascript - 在动态输入数据的下拉菜单中设置默认值

html - Bootstrap 导航栏布局错误

css - 在固定区域垂直对齐文本

html - 我该如何设置这样的 div 样式?

javascript - 如何减小 AspNet.ApplicationCookie 的大小以及其中的内容?

javascript - JS window.onload 用法与文档

php - 如何将 Google 云端硬盘图片嵌入网页?

html - xhtml2pdf 在创建 pdf 时不应用 css(html 到 pdf)

javascript - 从另一个模块导入 PropType 值会导致未定义

javascript - 微服务架构和MySQL数据库分页