gpt4 book ai didi

javascript - 从 URL 在网页内执行 javascript 函数?

转载 作者:行者123 更新时间:2023-11-30 12:18:40 34 4
gpt4 key购买 nike

我有这个 HTML 代码:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to display an alert box:</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
alert("I am an alert box!");
}
</script>

</body>
</html>

然后我将这段代码保存为:C:/js.html

然后我在浏览器的地址栏中写:

file:///C:/js.html#javascript:myFunction();

但是Javascript函数没有执行。为什么?

我怎样才能让它工作?

最佳答案

试试这个短片。使用 #test 作为 url index.html#test 的位置部分访问您的页面。

function myHash() {
alert('here iam!');
}

function hash() {
var hash = location.hash;

switch(hash) {
case '#test' : myHash();
break;
default : break;
}
}

hash();

关于javascript - 从 URL 在网页内执行 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687911/

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