HTML - javascript代码合并

时间:2014-05-01 11:35:45

标签: javascript html css

我试图将两个html / css-js代码合并在一起,但它无法正常工作。

第一个html代码是点击时更改的简单按钮文字:

<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" >

我要合并的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>

如何点击按钮更改文本,然后通过计时器重定向页面?

2 个答案:

答案 0 :(得分:1)

将附加代码添加到您已有的功能中:jsFiddle Example

<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>

答案 1 :(得分:0)

你的意思是这样吗?

<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" >