Jquery $(this).text() 不返回全文



我在jQuery上遇到了一个问题。我想获取span标签的文本并设置为图像元素的工具提示。使用$(this).text()我可以在使用警报时看到全文并且工作正常。现在,当我将鼠标悬停在图像上时,它不会在空格后显示文本,如果有任何前导空格,它不会显示任何内容。请参阅以下示例。

<script>
    $(document).ready(function(){
        $("span[id*='name']").each(function(){
            var telValue=$(this).text();
            alert(telValue);
            $(this).after("<img title=" +telValue+" src='path'" ); 
        }); 
    }); 
</script>
<span title="FirstNAME" id="Fname" style="width: 100%;" f2="C;40"> First Name is test</span>

请尝试下面的JS示例

$("span[id*='name']").each(function() {
var Value=$(this).text();
 
  alert(Value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="name1"> test 1</span>
<span id="name2"> test 2</span>
<span id="name3"> test 3</span>

最新更新