0

I am getting data from database that has single quotes in it. There are no problems in getting that, I get variable with single quotes in it

ex:

var title="vicky's blog";

Now the problem is I am not able to append this variable as title for anchor tag.

I have tried using escape, unescape, replacing with regex and all that. I think i am missing out something, please help.

http://jsfiddle.net/vigneshvdm/mtXSZ/

Please refer to this jsfiddle. Inspect the anchor tag there its not having the full variable as title.

I also tried solutions provided in

Using JavaScript single and double quotes for href's

jQuery single quote in JSON response

4

4 回答 4

4

您需要将标题值包装""在 html 元素中

$("#testing").html('<a href="#" title="'+unescape(title)+'">hello</a>');

演示:小提琴

但我会推荐

var title="vicky's blog's test dd \" with";

$('<a />', {
    href: '#', 
    title: title,
    html: 'test'
}).appendTo('#testing')

演示:小提琴

于 2013-06-17T03:54:41.787 回答
2

Just create the anchor with jQuery and let it solve the escaping issue:

var $anchor = $('<a>', {
    href: '#',
    title: title,
    text: 'hello'
});
$('#testing').html($anchor);

Demo

于 2013-06-17T03:55:47.573 回答
0

看到这个我认为它会帮助你: 在此处输入图像描述

于 2013-06-17T13:58:52.547 回答
0

如果要将文本放入 html 中,则应始终对特殊字符进行 html 编码。

'"<>&

var title="vicky&apos;s blog";
于 2013-06-17T03:55:00.290 回答