gpt4 book ai didi

javascript - 我正在尝试使用按钮在 Chrome 中使用 HTML 编辑网页的宽度和高度

转载 作者:行者123 更新时间:2023-11-28 05:24:16 24 4
gpt4 key购买 nike

考虑我当前拥有的代码:

<!DOCTYPE html>
<html>
<body>


<input id="but" name="but" type=Button onclick="changeBCol();" value="Shuffle Background Colour"></input>
<br></br>
<button style="background-color:transparent;width:255;height:255" onclick = "w = w+1">Window width up</button>
<br></br>
<button style="background-color:transparent;width:255;height:255">Window width down</button>
<br></br>
<button style="background-color:transparent;width:255;height:255">Window height up</button>
<br></br>
<button style="background-color:transparent;width:255;height:255">Window height down</button>

<script>
var w = window.innerWidth
</script>

</html>
</body>

目前无法使用,如有任何建议,我们将不胜感激。

最佳答案

因为您只是更改变量 w 的值(它不包含引用,而是包含值),而不是窗口的宽度或高度。另外您只能更改由某些脚本打开的窗口的宽度和高度。我认为尝试更改窗口的宽度和高度不是个好主意,但如果您想这样做,请考虑使用 window.resizeTo() .

加上:

  1. 更改<br></br><br> , <input></input><input> 。他们是void elements ,没有结束标签。
  2. 内为内,外为外:应该是<html>...<body>...</body></html> .
  3. 您应该指定<button> s' 类型,因为默认值为 type="submit" .

示例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="author" content="K.">

<title>Demo</title>
</head>

<body>
<div id="demoButtons">
<button id="incWidth" type="button">Increase its width</button>
<button id="decWidth" type="button">Decrease its width</button>
<button id="incHeight" type="button">Increase its height</button>
<button id="decHeight" type="button">Decrease its height</button>
</div>

<script>
let width = 100, height = 100;
childWindow = window.open("", "", "width=100, height=100");
childWindow.focus();

document.getElementById("demoButtons").addEventListener("click", event => {
console.log(event.target.id);
/**/ if(event.target.id === "incWidth") width += 100;
else if(event.target.id === "decWidth" && width >= 100) width -= 100;
else if(event.target.id === "incHeight") height += 100;
else if(event.target.id === "decHeight" && height >= 100) height -= 100;
console.log(width, height);

childWindow.resizeTo(width, height);
childWindow.document.textContent = `The width: ${width}, the height: ${height}.`;
childWindow.focus();
});
</script>
</body>
</html>

关于javascript - 我正在尝试使用按钮在 Chrome 中使用 HTML 编辑网页的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40296512/

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