Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript在页面加载时不工作_Javascript_Html - Fatal编程技术网

JavaScript在页面加载时不工作

JavaScript在页面加载时不工作,javascript,html,Javascript,Html,我正在处理一个html页面,现在我正在尝试将一个div样式重写为另一个样式,它看起来是这样的。为此,我使用如下java脚本 <script> window.onload=document.getElementsByClassName('fs-stretcher')[0].setAttribute("style","width: 0px; height: 0px; background-color: yellow;") </script> window.

我正在处理一个html页面,现在我正在尝试将一个div样式重写为另一个样式,它看起来是这样的
。为此,我使用如下java脚本

<script>
    window.onload=document.getElementsByClassName('fs-stretcher')[0].setAttribute("style","width: 0px; height: 0px; background-color: yellow;")
    </script>

window.onload=document.getElementsByClassName('fs-stretcher')[0].setAttribute(“样式”,“宽度:0px;高度:0px;背景色:黄色;”)

但它不起作用。请帮助我解决此问题。

您必须为
窗口指定函数引用。onload

window.onload = function () {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};

您的代码正在执行语句
document.getElementsByClassName….
并将
setAttribute
返回的值分配给
onload

您必须将函数引用分配给
window.onload

window.onload = function () {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};

您的代码正在执行语句
document.getElementsByClassName….
并将
setAttribute
返回的值分配给
onload

您必须将函数引用分配给
window.onload

window.onload = function () {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};

您的代码正在执行语句
document.getElementsByClassName….
并将
setAttribute
返回的值分配给
onload

您必须将函数引用分配给
window.onload

window.onload = function () {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};

您的代码正在执行语句
document.getElementsByClassName…
,并将
setAttribute
返回的值赋给
onload
另一种在div之后放置脚本的方法

<html>
<head>
</head>
<body>
<div class="fs-stretcher" style="width: 1170px; height: 480px;">adv</div>
<script>
    window.onload = document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 100px; height: 100px; background-color: yellow;")
</script>
</body>
</html>

副词。
window.onload=document.getElementsByCassName('fs-stretcher')[0].setAttribute(“样式”,“宽度:100px;高度:100px;背景色:黄色;”)

这也很好。但是如果您使用
window.onload=function(){----------}
是最好的方法,正如上面的答案所示。

在div之后放置脚本的另一种方法

<html>
<head>
</head>
<body>
<div class="fs-stretcher" style="width: 1170px; height: 480px;">adv</div>
<script>
    window.onload = document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 100px; height: 100px; background-color: yellow;")
</script>
</body>
</html>

副词。
window.onload=document.getElementsByCassName('fs-stretcher')[0].setAttribute(“样式”,“宽度:100px;高度:100px;背景色:黄色;”)

这也很好。但是如果您使用
window.onload=function(){----------}
是最好的方法,正如上面的答案所示。

在div之后放置脚本的另一种方法

<html>
<head>
</head>
<body>
<div class="fs-stretcher" style="width: 1170px; height: 480px;">adv</div>
<script>
    window.onload = document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 100px; height: 100px; background-color: yellow;")
</script>
</body>
</html>

副词。
window.onload=document.getElementsByCassName('fs-stretcher')[0].setAttribute(“样式”,“宽度:100px;高度:100px;背景色:黄色;”)

这也很好。但是如果您使用
window.onload=function(){----------}
是最好的方法,正如上面的答案所示。

在div之后放置脚本的另一种方法

<html>
<head>
</head>
<body>
<div class="fs-stretcher" style="width: 1170px; height: 480px;">adv</div>
<script>
    window.onload = document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 100px; height: 100px; background-color: yellow;")
</script>
</body>
</html>

副词。
window.onload=document.getElementsByCassName('fs-stretcher')[0].setAttribute(“样式”,“宽度:100px;高度:100px;背景色:黄色;”)

这也很好。但是,如果您使用
window.onload=function(){----------}
是上述答案的最佳方法。

window-onload是一个函数,您应该需要将一个函数绑定到此事件

因此,请尝试:

window.onload = function () {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
或者您可以创建一个函数

function setElementOnLoad() {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
并将其绑定到body标签的onload上:

<body onload="setElementOnLoad()">

window onload是一个函数,您应该需要将一个函数绑定到此事件

因此,请尝试:

window.onload = function () {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
或者您可以创建一个函数

function setElementOnLoad() {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
并将其绑定到body标签的onload上:

<body onload="setElementOnLoad()">

window onload是一个函数,您应该需要将一个函数绑定到此事件

因此,请尝试:

window.onload = function () {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
或者您可以创建一个函数

function setElementOnLoad() {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
并将其绑定到body标签的onload上:

<body onload="setElementOnLoad()">

window onload是一个函数,您应该需要将一个函数绑定到此事件

因此,请尝试:

window.onload = function () {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
或者您可以创建一个函数

function setElementOnLoad() {
    document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
并将其绑定到body标签的onload上:

<body onload="setElementOnLoad()">


为您的
窗口.onload
事件仅使用该功能是非常危险的。更好的方法是将
脚本
标记放置在靠近结束正文标记的位置
。到那时,这些元素已经被加载了。对于
窗口.onload
事件,仅使用该函数是非常危险的。更好的方法是将
脚本
标记放置在靠近结束正文标记的位置
。到那时,这些元素已经被加载了。对于
窗口.onload
事件,仅使用该函数是非常危险的。更好的方法是将
脚本
标记放置在靠近结束正文标记的位置
。到那时,这些元素已经被加载了。对于
窗口.onload
事件,仅使用该函数是非常危险的。更好的方法是将
脚本
标记放置在靠近结束正文标记的位置
。到那时,这些元素已经加载完毕。