JQuery:在 HTML 上使用 .text() ,警报在消息中显示奇怪的文本
JQuery: using .text() on an HTML ,the alert displays strange text in the message
我在警报中得到了标签的文本,文本附近有奇怪的符号。请告诉我如何删除这些奇怪的字符?我附上程序代码和照片。谢谢。
$('.blog_content_tags .tags li a').on('click', function () {
var name = $(this).text();
alert(name);
});
enter image description here
这些字符是 tab
s.
您可以通过在源代码中使用空格或使用 trim()
来删除它们
$('a').on('click', function () {
var name = $(this).text();
alert(name);
alert(name.trim());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href='#'>
test
</a>
我在警报中得到了标签的文本,文本附近有奇怪的符号。请告诉我如何删除这些奇怪的字符?我附上程序代码和照片。谢谢。
$('.blog_content_tags .tags li a').on('click', function () {
var name = $(this).text();
alert(name);
});
enter image description here
这些字符是 tab
s.
您可以通过在源代码中使用空格或使用 trim()
$('a').on('click', function () {
var name = $(this).text();
alert(name);
alert(name.trim());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href='#'>
test
</a>