我得到了这个代码片段,我想替换位于顶层和低级<div>元素之间的“到达”文本,但是我想不出一种方法。
我不想在replaceWith、html或类似的方法上使用<div class="left booktext">,因为这将取代html元素的其余部分。有一个附加到input class="bookfields"的事件处理程序,我不想失去它。不幸的是,没有任何活动授权。
<div class="left booktext">
ARRIVAL
<div class="bookborder">
<input type="hidden" name="checkin" value="2014-03-05">
<input type="text" class="bookfields hasDatepicker" style="width:65px;" id="checkin-select" placeholder="dd/mm/yyyy">
</div>
</div>发布于 2014-03-05 09:25:33
您可以使用contents()和replaceWith()
$('.left').contents().first().replaceWith("New Text");Fiddle Demo
发布于 2014-03-05 09:24:20
在纯Javascript:
var books = document.querySelectorAll('.booktext');
for (var i = 0, length = books.length; i < length; i++) {
books[i].firstChild.data = "your text";
}不过,使用jQuery更容易:
$('.booktext').contents().first().replaceWith("your text");咨询
最好将文本放在span元素中,这样做有所有的好处。
通过这种方式,您可以将文本按div内的任意顺序排列,例如:
<div class="left booktext">
<div class="bookborder">
<input type="hidden" name="checkin" value="2014-03-05">
<span>ARRIVAL</span>
<input type="text">
</div>
</div>而代之以:
$('.bookborder span').text('my new text');发布于 2014-03-05 10:10:36
在纯javascript中,您可以访问这样的文本节点
var bookText = document.querySelector(".booktext");
bookText.firstChild.nodeValue = "Some other text";看到它在这里起作用
http://codepen.io/sajjad26/pen/JkAms
https://stackoverflow.com/questions/22193330
复制相似问题