html - 按钮文本更改时保持按钮大小稳定
问题描述
我有一个bootstrap
按钮:
<div class="float-right" >
<label>show as </label>
<input class="btn btn-outline-primary rounded active" type="button" value="percentage" onclick="toggle(this)" />
</div>
我使用以下方法切换文本:
<script type="text/javascript">
function toggle(button) {
if(button.value=="percentage") {
button.value="units";
} else {
button.value="percentage";
}
}
</script>
当文本从较长的单词更改为较短的单词时,如何让按钮不调整大小。即我希望按钮保持与具有文本时相同的大小。percentage
units
percentage
注意我宁愿不使用硬编码大小style="min-width: 120px;"
。
解决方案
我建议使用javascript
.
选项之一:
<div class="float-right" >
<label>show as </label>
<input class="btn btn-outline-primary rounded active" type="button" value="percentage" onclick="toggle(this)" />
</div>
<script type="text/javascript">
function toggle(button) {
const buttonWidth = button.offsetWidth;
if(button.value=="percentage") {
button.value="units";
button.style.width = `${buttonWidth}px`;
} else {
button.value="percentage";
}
}
</script>
推荐阅读
- python - 循环遍历多个级别的子文件夹并将 csv.gz 文件转换为 csv
- javascript - 理解“scrollPlayerIntoView”函数背后的逻辑问题
- c# - 类型“System.Web.UI.WebControls.GridView”未标记为可序列化
- c++ - 如何使函数的返回类型与另一个函数相同?
- android - 将后缀添加到 WiFi Direct 组名称的名称。(类似于 PDANet 所做的)
- angular - 如何选择要发出的订阅观察者?
- r - 相当于R中“uniroot”中“优化”的“目标”?
- pandas - Pandas - 将 isin 应用于数据框时出错
- python - Keras:将模型对象作为参数传递给损失函数
- java - 如何重写此代码以使用 Stream 和功能 API?