原生JS实现一个按钮的点击事件根据不同条件响应不同函数

例如,一个按钮实现暂停与继续。(下面错误代码只是为了表达意思- 。-)

<input id="btn" type="button" value="开始">
<script>
window.onload=function(){
    document.getElementById('btn').onclick=function(){
        if(this.value="开始"){
            this.value="暂停";
        }esle{
            this.value="开始";
        }
    }
</script>

怎么实现当value值为“开始”时点击按钮将value值设为“暂停”,当value值为“暂停”时,点击按钮时将值设置为“开始”。即一个按钮根据条件执行不同功能。

设置一个自定义属性onOff,或者设置个类似的变量就可以了

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <script type="text/javascript">
        window.onload = function () {
            var oInp = document.getElementsByTagName('input')[0];
            oInp.onOff = false;

            oInp.onclick = function () {
                if (this.onOff) {
                    this.value = '开始';
                    this.onOff = false
                    //  doSomething……
                }else {
                    this.value = '暂停';
                    this.onOff = true;
                    //  doSomeOtherthing……
                }
            }
        }
    </script>
</head>
<body>
    <input type="button" value="开始">
</body>
</html>

用c:if试试....

  • - 被你搞糊涂了,,判断不是==这样么!!!你用一个=不成赋值了么 !!!!
 <input id="btn" type="button" value="开始">
<script>
window.onload=function(){
    document.getElementById('btn').onclick=function(){
        if(this.value=="开始"){
            this.value="暂停";
        }esle{
            this.value="开始";
        }
    }
</script>
  • - 被你搞糊涂了,,判断不是==这样么!!!你用一个=不成赋值了么 !!!!
 <input id="btn" type="button" value="开始">
<script>
window.onload=function(){
    document.getElementById('btn').onclick=function(){
        if(this.value=="开始"){
            this.value="暂停";
        }esle{
            this.value="开始";
        }
    }
</script>

第一个回答可以,可能楼主经验不足所有看不懂