gpt4 book ai didi

Javascript innerWidth 不工作

转载 作者:行者123 更新时间:2023-11-29 18:03:39 26 4
gpt4 key购买 nike

var x = window.innerWidth;
if (window.innerWidth<500){
function myFunction() {
var node = document.getElementById("myList2").firstChild;
document.getElementById("myList1").appendChild(node);
}}
var myVar = setInterval(myFunction, 1000);

document.getElementById("test").innerHTML = x;
<html>
<body>

<ul id="myList1"><li>Coffee</li><li>Tea</li></ul>
<ul id="myList2"><li>Water</li><li>Milk</li></ul>
<p id="test"></p>

</body>
</html>

我的宽度是 1366px 但上面的代码执行并将 li 添加到 myList1。任何帮助将不胜感激。可能这是一个新手问题,但我是编码新手。

最佳答案

你应该在函数中移动你的条件:

var x = window.innerWidth;
function myFunction() {
if ( x < 500 ) {
var node = document.getElementById("myList2").firstChild;
document.getElementById("myList1").appendChild(node);
}
}
var myVar = setInterval(myFunction, 1000);
document.getElementById("test").innerHTML = x;
<html>
<body>

<ul id="myList1"><li>Coffee</li><li>Tea</li></ul>
<ul id="myList2"><li>Water</li><li>Milk</li></ul>
<p id="test"></p>

</body>
</html>

关于Javascript innerWidth 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33085233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com