我有这个 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/