HTML属性中的Javascript变量

我知道有很多关于此的帖子,但是我找不到我的特定问题的答案。

我想让JS变量成为HTML属性的值

<script> var screenWidth = screen.width </script>

<img src="images/title.png" width="VARIABLE HERE" style="margin-top: 3%"`

VARIABLE HERE是我想要screenWidth变量去的地方。最好的办法是什么?

谢谢,本

回答:

这应该工作:

<script>var screenWidth = screen.width;</script> 

...

<img src="images/title.png" onload="this.width=screenWidth;" style="margin-top: 3%">

以上是 HTML属性中的Javascript变量 的全部内容, 来源链接: utcz.com/qa/429882.html

回到顶部