jquery - 将按钮的值设置为 HTML 内容

标签 jquery

如何更改按钮的值以便将其解释为 HTML 代码?我有这个:

<input type="button" value="Click">

当按下它时,我会这样做:

$(this).prop('disabled', true);
$(this).prop('value', '<i class="fa fa-spinner"></i>');

但是内容不会是旋转的旋转器。我该如何解决这个问题?

最佳答案

您不能将任何 HTML 放入 <input type="button"> 中元素。您需要使用<button>为了实现这一点,以及 html()而不是prop() :

$('button').click(function() {
  $(this).prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i>');
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button">Click</button>

请注意添加 fa-spin类使图标旋转。

关于jquery - 将按钮的值设置为 HTML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42487063/

相关文章:

javascript - 如何仅禁用/删除输入字段上的第一个空格?

javascript - jQuery "hover state"- 测试元素是否不再悬停在其上

javascript - 如何使用 jQuery 从 javascript 数组中获取最接近的元素?

php - 根据选择的运输有条件地隐藏 WooCommerce 中的“结账”字段

javascript - 在使用 Javascript 上传之前删除选定的文件

php - 使用 jquery autosuggest 从多个表中选择

jquery - 在 li 上添加类并在单击时添加类

javascript - 变量值发生变化(根据alert())但被插件代码忽略?

javascript - jQuery jPlayer 更改媒体不工作

jquery - 选择 html 元素内的文本并更改样式