简体   繁体   English

单击时更改按钮文本,再次单击时更改回

[英]changing button text when clicked and changing it back when clicked again

how can i change the text of a button when clicked? 单击后如何更改按钮的文字? and change it back when clicked again? 并在再次单击时将其更改回? i have this javascript i made.and its not giving me what i want.please help? 我有我制作的JavaScript,并且没有给我我想要的东西。请帮忙吗?

    $('#addbtn').click(function (){
    if(document.getElementById('addMat').style.display == "none"){
        document.getElementById('addMat').style.display= "inline";
        $(this).val('Cancel');
    }else
        document.getElementById('addMat').style.display= "none";
        $(this).val('Add Material');
});

Just do it all with jQuery syntax: 只需使用jQuery语法即可:

$('#addbtn').click(function (){
    var addMat = $('#addMat');
    if( addMat.css('display')  === 'none' ) {
      addMat.css('display','inline');
      $(this).val('Cancel');
    } else {
      addMat.css('display','none');
      $(this).val('Add Material');
    }
});
$('#addbtn').click(function (){
    if(document.getElementById('addMat').style.display === "none"){
        document.getElementById('addMat').style.display = "inline";
        $(this).attr("value","Cancel");
    }else{
        document.getElementById('addMat').style.display = "none";
        $(this).attr("value","Add Material");
    }
});

or Better... 或更好...

$('#addbtn').click(function (){
    if( $('#addMat').css('display')  === 'none' ){
      $('#addMat').css('display','inline');
      $(this).attr("value","Cancel");
    } else {
      $('#addMat').css('display','none');
      $(this).attr("value","Add Material");
    }
});
$(this).prop('value', 'Cancel'); 

or 要么

$(this).html('Cancel');

The Best way to do it is create a button and put a span tag around the text 最好的方法是创建一个按钮,并在文本周围放置一个span标签

$("span", this).text("My NEW Text");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM