无法获得<rect element =“”的宽度=“”<svg

时间:2017-01-19 05:17:11

标签: javascript jquery css highcharts uncaught-typeerror

=“”

我正在尝试访问上一个"rect"这样我就可以得到它的宽度和高度。我尝试了关注jquery $(".highcharts-plot-background").width(),但我得到的答案都是0.还尝试使用$(".highcharts-plot-background")[0].width(),但后来我收到以下错误

  

未捕获的TypeError:$(...)。width()不是函数。

我愿意接受建议。

<svg version="1.1" class="highcharts-root" xmlns="http://www.w3.org/2000/svg" width="710" viewBox="0 0 710 325" height="325">
  <desc>Created with Highcharts 5.0.0</desc>
  <defs>
    <clipPath id="highcharts-1">
      <rect x="0" y="0" width="636" height="238" fill="none"></rect>    </clipPath>
  </defs>
  <rect fill="#ffffff" class="highcharts-background" x="0" y="0" width="710" height="325" rx="0" ry="0"></rect>
  <rect fill="none" class="highcharts-plot-background" x="64" y="50" width="636" height="238" style="margin:0px;"></rect>
</svg>

1 个答案:

答案 0 :(得分:1)

使用attr("width")代替width()。如果您已经在css中声明了宽度,那么width()就可以正常工作。

请参阅下面的代码段。

console.log($(".highcharts-plot-background").attr("width"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<svg version="1.1" class="highcharts-root" xmlns="http://www.w3.org/2000/svg" width="710" viewBox="0 0 710 325" height="325">
  <desc>Created with Highcharts 5.0.0</desc>
  <defs>
    <clipPath id="highcharts-1">
      <rect x="0" y="0" width="636" height="238" fill="none"></rect>    </clipPath>
  </defs>
  <rect fill="#ffffff" class="highcharts-background" x="0" y="0" width="710" height="325" rx="0" ry="0"></rect>
  <rect fill="none" class="highcharts-plot-background" x="64" y="50" width="636" height="238" style="margin:0px;"></rect>
</svg>