2017-08-24 26 views
0

我有警告标志字符在HTML实体(十进制)中的值为⚠,我试图从所有复选框的标签文本中删除它。使用jquery从所有标签文本中删除特定的html实体字符

例如,在HTML中,我输入的标签之一如下:

我如何使用$ (JQuery的)选择,从这些标签取下⚠ HTML实体。

这里是我的尝试:

$('input:checkbox').each(function (index) { 
 
    // I thought by splitting that part and then joining it 
 
    // with an empty string would work. 
 
    $(this).next().text().split('⚠').join(''); 
 
});
<input id='input1'/><label for='input1'>This is the warning sign &#9888;</label>

+0

我看到的输入,而无需在例如一个类型= “复选框”。 – Taplar

+0

此外,你正在得到的字符串,分裂它,并加入它,然后没有采取任何结果。该字段的text()不会自动更新为新值。如果您想更新,必须将其设回。 – Taplar

回答

2

将实际字符保留在拆分函数中而不是html实体中。

$('input:checkbox').each(function(index){  
 
    $(this).next().text($(this).next().text().split('⚠').join('')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input id='input1' type="checkbox"/><label for='input1'>This is the warning sign &#9888;</label>

0

您可以使用split()slice(),其次是join()实现这一点,假设这个字符总是在字符串的结尾:

$(document).ready(function() { 
 
    $('input:checkbox').each(function(index) { 
 
    var yourText = $(this).next().text(); 
 
    var convertedText = yourText.split(' ').slice(0, -1).join(' '); 
 
    console.log(convertedText); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type='checkbox' id='input1' /><label for='input1'>This is the warning sign &#9888;</label>

0

你可以保留实际的HTML字符:

$('input:checkbox').each(function(index){  
 
    $(this).next().text($(this).next().text().replace('⚠','')); 
 
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> 
 
<input id='input1' type="checkbox"/><label for='input1'>This is the warning sign &#9888;</label>

相关问题