用其他字符串替换 nextSibling
Replacing nextSibling with other string
我有以下跨度元素:
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
使用下面的js函数,我可以得到</span>
和</div>
之间的长度:
$('span.inside').each(function() {
var sphs = $(this).get(0).nextSibling;
var len = sphs.length;
//If the length is less than 2
if(len < 2){
// i want to replace it with another words like "thanks."
}
})
如果长度小于8,那么我想在</span>
和</div>
之间用另一个词或字符串(例如thanks.
)替换它(参见单词 "thanks" 位置)。
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>thanks.</div>
</div>
如何用另一个字符串替换它?
编辑:
请注意,我使用的是 .each
,因为可能有多个 span 元素具有相同的 class 名称。在第一行,只有一个句点(</span>.</div>
),因此它满足条件,将被替换为另一个词(在本例中为"thanks")。
第二行(</span>name</div>
),长度大于2,不会被替换
我希望这能说明问题。
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
...将成为...
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span> thanks.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
添加一个额外的(空)span 并设置它。设置span的text()就这么简单。
HTML:
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span><span id="extra"></span></div>
</div>
JS:
//If the length is less than 2
if(len < 2){
$('span#extra').text("thanks.");
}
$('span.inside').each(function() {
var sphs = $(this).get(0).nextSibling;
var len = sphs.length;
//If the length is less than 2
if(len < 2){
// DOM method:
sphs.textContent = ' Thanks.';
// jQuery method:
$(this).parent().get(0).childNodes[2].textContent = ' Thanks.';
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>.</div>
<div class="outside">Type here <span class="inside"> please</span> ok?</div>
</div>
或者在这里试试
https://jsfiddle.net/8pa4akzh/3/
我有以下跨度元素:
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
使用下面的js函数,我可以得到</span>
和</div>
之间的长度:
$('span.inside').each(function() {
var sphs = $(this).get(0).nextSibling;
var len = sphs.length;
//If the length is less than 2
if(len < 2){
// i want to replace it with another words like "thanks."
}
})
如果长度小于8,那么我想在</span>
和</div>
之间用另一个词或字符串(例如thanks.
)替换它(参见单词 "thanks" 位置)。
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>thanks.</div>
</div>
如何用另一个字符串替换它?
编辑:
请注意,我使用的是 .each
,因为可能有多个 span 元素具有相同的 class 名称。在第一行,只有一个句点(</span>.</div>
),因此它满足条件,将被替换为另一个词(在本例中为"thanks")。
第二行(</span>name</div>
),长度大于2,不会被替换
我希望这能说明问题。
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
...将成为...
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span> thanks.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
添加一个额外的(空)span 并设置它。设置span的text()就这么简单。
HTML:
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span><span id="extra"></span></div>
</div>
JS:
//If the length is less than 2
if(len < 2){
$('span#extra').text("thanks.");
}
$('span.inside').each(function() {
var sphs = $(this).get(0).nextSibling;
var len = sphs.length;
//If the length is less than 2
if(len < 2){
// DOM method:
sphs.textContent = ' Thanks.';
// jQuery method:
$(this).parent().get(0).childNodes[2].textContent = ' Thanks.';
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>.</div>
<div class="outside">Type here <span class="inside"> please</span> ok?</div>
</div>
或者在这里试试 https://jsfiddle.net/8pa4akzh/3/