单击时如何更改按钮的文本?并在再次单击时将其更改回?我有这个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');
});
只需使用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");
}
});
或者更好。。。
$('#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');
或
$(this).html('Cancel');
最好的方法是创建一个按钮,并在文本周围放置一个跨度标签
$("span", this).text("My NEW Text");