我有这样的标记:
<li data-id="1">
<button type="button" class="btn btn-default btn-md" style="margin: .25em; padding: .25em;">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
random text
<input type="hidden" name="global_filter[default_fields][][apple]" value="random text_again">
</button>
</li>
我想将文本“随机文本”更改为“其他内容”。
我能够找到文本节点:
$li.find('button').first().contents().filter(function() {
return this.nodeType == 3;
}).text();
但我怎样才能更改文字?当我将参数传递给像text('something else')
这样的文本时,没有任何变化。
答案 0 :(得分:1)
我可能会将文本放在自己的容器中,如
<div id='changeableText'>Random Text</div>
然后直接使用
访问它 $('#changeableText').text('New text!');
实现这一目标。
答案 1 :(得分:0)
<强>更新强>
您可以选择仅按下按钮的text()
并进行更改。
注意:如果存在多个非标记文本,则无效。 E.g。
<li data-id="1">
<button type="button" class="btn btn-default btn-md" style="margin: .25em; padding: .25em;">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
random text
<input type="hidden" name="global_filter[default_fields][][apple]" value="random text_again">
another random text
</button>
</li>
否则她就是了 工作演示:
var getNonMarkedUpText = $('button').text().replace(/^\s+|\s+$|\s+(?=\s)/g, "");
$('button').html($('button').html().replace(getNonMarkedUpText, 'something else'));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li data-id="1">
<button type="button" class="btn btn-default btn-md" style="margin: .25em; padding: .25em;">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
random text
<input type="hidden" name="global_filter[default_fields][][apple]" value="random text_again">
</button>
</li>
&#13;
答案 2 :(得分:0)
试试这个:
<li data-id="1">
<button type="button" class="btn btn-default btn-md" style="margin: .25em; padding: .25em;">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
<span id="txt1">random text</span>
<input type="hidden" name="global_filter[default_fields][][apple]" value="random text_again">
</button>
</li>
那样,你可以去:
document.getElementById("txt1").innerText = "txet modnar";
或者
$("#txt1").text("tandom rext");