I have the following HTML :
我有以下HTML:
<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .
<label for="element_18_1">Dollars</label>
</span>
I would like to remove the dot between the input and the label tags using jQuery.
我想使用jQuery删除输入和标签标签之间的点。
I tried this code with no result : $('#element_18_1').contents(':gt(2)').remove();
Thanks
我尝试了这段代码没有结果:$('#element_18_1')。contents(':gt(2)')。remove();谢谢
5 个解决方案
#1
1
You could, however always choose an easier way, a simpler way, a clan slate readable and understandable way, a pure JavaScript way.
然而,您可以选择更简单的方式,更简单的方式,可靠且易懂的方式,纯粹的JavaScript方式。
element_18_1.nextSibling.data=" ";
element_18_1.nextSibling.data=" ";
<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .
<label for="element_18_1">Dollars</label>
</span>
#2
1
This removes all texts which are not inside a tag (as element)
这将删除所有不在标记内的文本(作为元素)
$(document).ready(function(){
current = $('#element_18_1');
siblings = $('#element_18_1').siblings();
parent = $('#element_18_1').parent();
$(parent).html('');
$(parent).append(current).append(siblings);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .
<label for="element_18_1">Dollars</label>
</span>
You can make it shorter by joining some lines. I tried to show it step by step
你可以加入一些线来缩短它。我试着一步一步地展示它
#3
0
You can use contents() and slice(), i.e.:
你可以使用contents()和slice(),即:
var el1 = $("#element_18_1"), el2 = $("#element_18_1 + label"), contents = el1.parent().contents();
contents.slice(contents.index(el1) + 1, contents.index(el2)).remove();
el1.after(" ");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .
<label for="element_18_1">Dollars</label>
</span>
#4
0
As stated here, I suggest to filter out the text node(s), and remove them:
如上所述,我建议过滤掉文本节点,然后删除它们:
$('span')
.contents()
.filter(function() {
return this.nodeType == 3; //Node.TEXT_NODE
}).remove();
#5
0
This code is a bit crude but it should work
这段代码有点粗糙,但应该可行
$("span:contains('.')").html($("span:contains('.')").html().replace('.',''));
#1
1
You could, however always choose an easier way, a simpler way, a clan slate readable and understandable way, a pure JavaScript way.
然而,您可以选择更简单的方式,更简单的方式,可靠且易懂的方式,纯粹的JavaScript方式。
element_18_1.nextSibling.data=" ";
element_18_1.nextSibling.data=" ";
<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .
<label for="element_18_1">Dollars</label>
</span>
#2
1
This removes all texts which are not inside a tag (as element)
这将删除所有不在标记内的文本(作为元素)
$(document).ready(function(){
current = $('#element_18_1');
siblings = $('#element_18_1').siblings();
parent = $('#element_18_1').parent();
$(parent).html('');
$(parent).append(current).append(siblings);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .
<label for="element_18_1">Dollars</label>
</span>
You can make it shorter by joining some lines. I tried to show it step by step
你可以加入一些线来缩短它。我试着一步一步地展示它
#3
0
You can use contents() and slice(), i.e.:
你可以使用contents()和slice(),即:
var el1 = $("#element_18_1"), el2 = $("#element_18_1 + label"), contents = el1.parent().contents();
contents.slice(contents.index(el1) + 1, contents.index(el2)).remove();
el1.after(" ");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .
<label for="element_18_1">Dollars</label>
</span>
#4
0
As stated here, I suggest to filter out the text node(s), and remove them:
如上所述,我建议过滤掉文本节点,然后删除它们:
$('span')
.contents()
.filter(function() {
return this.nodeType == 3; //Node.TEXT_NODE
}).remove();
#5
0
This code is a bit crude but it should work
这段代码有点粗糙,但应该可行
$("span:contains('.')").html($("span:contains('.')").html().replace('.',''));