html-javascript代码合并
我正在尝试将两个html/css-js代码合并在一起,但不起作用 第一个html代码是单击时更改的简单按钮文本:html-javascript代码合并,javascript,html,css,Javascript,Html,Css,我正在尝试将两个html/css-js代码合并在一起,但不起作用 第一个html代码是单击时更改的简单按钮文本: <html> <head> <script type="text/javascript"> function myTest(btnID) { document.getElementById(btnID).value="After"; } </script> </head> <body> <inpu
<html>
<head>
<script type="text/javascript">
function myTest(btnID)
{
document.getElementById(btnID).value="After";
}
</script>
</head>
<body>
<input type="button" name="toggleBtn" id="toggleBtn" onclick="myTest('toggleBtn')" value="Before" >
功能测试(btnID)
{
document.getElementById(btnID).value=“After”;
}
以及我要合并的JavaScript代码:
<script type="text/JavaScript">
<!--
redirectTime = "5000";
redirectURL = "http://cpanel.hostinger.ae";
function timedRedirect() {
setTimeout("location.href = redirectURL;",redirectTime);
}
// -->
</script>
<div style="background-color:green;padding:5px;width:110px;cursor:pointer;"
onclick="JavaScript:timedRedirect()">
By M.A JS Code
</div>
作者:M.A JS代码
我怎样才能使单击按钮更改文本,然后通过计时器重定向页面?你的意思是这样吗
<html>
<head>
<script type="text/javascript">
function myTest(btnID)
{
document.getElementById(btnID).value="After";
redirectTime = 5000;
redirectURL = "http://cpanel.hostinger.ae";
(function(redirectTime, redirectURL) {
setTimeout("location.href = redirectURL;",redirectTime);
})(redirectTime, redirectURL);
}
</script>
</head>
<body>
<input type="button" name="toggleBtn" id="toggleBtn" onclick="myTest('toggleBtn')" value="Before" >
功能测试(btnID)
{
document.getElementById(btnID).value=“After”;
重新定向时间=5000;
重定向URL=”http://cpanel.hostinger.ae";
(函数(重定向时间、重定向URL){
setTimeout(“location.href=redirectURL;”,redirectTime);
})(重定向时间,重定向URL);
}
将附加代码添加到您已有的功能中:
功能测试(e){
e、 value=“After”;
重定向时间=“5000”;
重定向URL=”http://cpanel.hostinger.ae";
setTimeout(函数(){location.href=redirectURL;},redirectTime);
}
在您的代码中,timedDirect()
将永远不会执行。我怀疑,这是OP想要的。是的,我刚刚意识到,他只是要求代码合并,这就是我所做的,但是我解决了这个问题(将代码移出函数,因为它觉得不必要),这也不起作用。setTimeout()
回调将在全局范围内执行,其中redirectURL
变量未知。最好在这里使用匿名函数。它在JSFIDLE中工作,但如果使用JSHint,它会抱怨这行不是最佳实践。隐含评估。
<html>
<head>
<script type="text/javascript">
function myTest(e) {
e.value = "After";
redirectTime = "5000";
redirectURL = "http://cpanel.hostinger.ae";
setTimeout(function(){location.href = redirectURL;}, redirectTime);
}
</script>
</head>
<body>
<input type="button" name="toggleBtn" id="toggleBtn" onclick="myTest(this);" value="Before">
</body>
</html>