我不知道这是否是 JavaScript、jQuery 或 JSON 的问题,但是当我在我的任何数据条目中使用空格时(请参阅下面的 var title1)然后空格后的所有内容被省略。也许数据必须以某种方式编码?我尝试使用   ;而不是运气不好的实际空间。
为了简单起见,我尝试删除与此问题无关的所有代码。
html\javascript:
<div id="verification"></div>
<script>
function update() {
$.ajax({
url: 'update.php', //php
data: "",
dataType: 'json',
success: function (data) {
//on receive of reply
var title1 = data[2];
$('#verification').html("<img src=images/data-image.gif title="+title1+"></img>"); //output to html
}
});
}
</script>
json 响应
["这是一个非常长的测试,有 123 个数字和字母。* . 测试"]
输出(通过带有标题的鼠标悬停文本)
这个
*****php******
$result = mysql_query("SELECT title1 FROM users WHERE username = '$foobar'")
or die(mysql_error());
$array = mysql_fetch_row($result);
echo json_encode($array);
最佳答案
您在属性定义中缺少引号,您最终的 HTML 字符串将以如下形式结束:
<img src=images/data-image.gif title=this is a really long test that has 123 numbers and letters. * . test></img>
这就是为什么只有第一个词出现在 title
属性中的原因。
为了避免转义问题,我建议使用 jquery 创建元素,例如:
var $img = $('<img>').attr('src', 'images/data-image.gif').attr('title', title1);
$('#verification').append($img);
关于javascript - 空格导致我的 javascript 显示不正确( trim 输出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32289704/