无法读取null的属性“样式”-未捕获的类型错误

时间:2019-02-02 09:29:20

标签: javascript html

我收到错误消息“无法读取null的样式'style'”,我尝试了将脚本放在HTML底部的所有操作,但仍然出现错误。我该怎么办?

错误行:

document.getElementById("span").style.webkitTransform = v 
"rotate"+p+"("+ps.value+"deg)"

function r(p) {
  var ps;
  ps = document.getElementById(p);
  document.getElementById("span").style.webkitTransform = "rotate" + p + "(" + ps.value + "deg)"
}
<div class="box">
  <div class="div">
    <span class="span"></span>
  </div>
</div>
X:<input type="range" id="X" onchange="r('X');" value="45" /> Y:
<input type="range" id="Y" onchange="r('Y')" /> Z:
<input type="range" id="Z" onchange="r('Z')" />

1 个答案:

答案 0 :(得分:0)

  1. 您没有ID,但有一个课程
  2. 您不需要尝试转换跨度之类的内联元素

How can I use CSS3 transform on a span?

function r(p) {
  var ps;
  ps = document.getElementById(p);
  var rot = "rotate" + p + "(" + ps.value + "deg)";
  console.log(rot);
  document.querySelector(".div").style.webkitTransform = rot;
}
<div class="box">
  <div class="div">
    <span class="span">shfasjdfhalsdjfh</span>
  </div>
</div>
X:<input type="range" id="X" onchange="r('X');" value="45" /> Y:
<input type="range" id="Y" onchange="r('Y')" /> Z:
<input type="range" id="Z" onchange="r('Z')" />