我有一个简单的表单添加元素到数据库。我使用相同的表单进行编辑。我需要将按钮文本从add item更改为edit item。我不知道如何在没有触摸span的情况下只编辑span之后的文本。
<button type="submit" id="form-submit" class="btn btn-md pull-right btn-info">
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Add item
</button>我想要这个:
<button type="submit" id="form-submit" class="btn btn-md pull-right btn-info">
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Edit item
</button>发布于 2016-02-17 00:31:58
不需要费力地选择特定的文本片段,而是用label包装它,然后通过jQuery选择它。更优雅,更具可读性。
发布于 2016-02-16 18:48:06
更好的做法是直接修改HTML。假设您不能这样做,那么您需要获取button的textNode并对其进行修改。试试这个:
$('button').contents().filter(function() {
return this.nodeType == 3 && this.nodeValue.trim() != '';
}).get(0).nodeValue = 'Edit item';发布于 2016-02-16 18:50:47
您可以在textContent中使用lastChild。
Live Demo
var txt = document.getElementById('form-submit').lastChild.textContent;您可以使用childNodes来获取特定的子级
var txt = document.getElementById('form-submit').childNodes[2].textContent;https://stackoverflow.com/questions/35430399
复制相似问题