Javascript中字符串输出html的动态链接
在写一个应用时,碰到一个问题,我从getJSON里面返回的变量无法放入以字符串形式表示的html中。
$.getJSON('getData.php', {data:"test"}, function(result) {
$.each(result, function() {
var tr = '<a style="text-decoration:none;" href="http://www.google.com.hk?q="' + this[0] + '>' + this[0] + '</tr>';
$(tr).appendTo("#test");
});
});
但是这样发现href中q的值取不到,整个tr的内容在Firefox中被解析为<a test.value="" href="http://www.google.com.hk?q=" style="text-decoration:none;">test.value</a>。链接并不是动态的。
因为不知道Javasript是如何处理这种情况的,于是改变思路,先将链接生成好,然后放在href中。
$.getJSON('getData.php', {data:"test"}, function(result) {
$.each(result, function() {
var href = "http://www.google.com.hk?q=" + this[0];
var tr = '<a style="text-decoration:none;" href=' + href + '>' + this[0] + '</tr>';
$(tr).appendTo("#test");
});
});
问题解决!
浙公网安备 33010602011771号