如何在Javascript中删除on和off addEventListner

时间:2018-06-30 12:55:24

标签: javascript

大家好,我是关于javascript的新知识,这是我的CSS .done {text-decoration: line-through;},这是脚本:

<h1>Testing</h1>
<script>
    var h1 = document.querySelector('h1');
    h1.addEventListener("click", lineThrough);
    function lineThrough(){
        event.target.className='done';
    }
    // h1.addEventListener("click", reset);
    // function reset(){
    //  h1.className="";
    // }
</script>

我只想单击一下,即可打开和关闭.done。我需要解决方案。谢谢

1 个答案:

答案 0 :(得分:0)

使用classList.toggle方法

var h1 = document.querySelector('h1');
h1.addEventListener("click", lineThrough);

function lineThrough() {
  event.target.classList.toggle('done');
}
.done {
  color: green;
  text-decoration: line-through;
}
<h1>Testing</h1>