为什么更改此文本会破坏我的Javascript

时间:2017-11-21 20:12:45

标签: javascript html css

我有一些javascript可以更改按钮内的文本,如下所示:



$('#studentDocument_File_EditShow').click(function() {
  if ($(this).html().indexOf('Cancel') > 0) {
    $(this).html('<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit');
  } else {
    $(this).html('<span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Cancel');
  }
});
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="studentDocument_File_EditShow" type="button" class="btn btn-default"><span class="glyphicon glyphicon-remove" 
    aria-hidden="true"></span> Cancel</button>
&#13;
&#13;
&#13;

但是,如果我更改&#34;取消&#34;第5行中的文字,我的html没有恢复。

我怎么能解决这个问题,所以我可以只有一个没有文字的glyphicon?

1 个答案:

答案 0 :(得分:0)

您需要更改indexOf('Cancel')

中的文字

见下文。

$('#studentDocument_File_EditShow').click(function() {
      if ($(this).html().indexOf('No longer edit') > 0) {
        $(this).html('<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit');
      } else {
        $(this).html('<span class="glyphicon glyphicon-remove" aria-hidden="true"></span> No longer edit');
      }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="studentDocument_File_EditShow" type="button" class="btn btn-default"><span class="glyphicon glyphicon-remove" 
    aria-hidden="true"></span> Cancel</button>