使用javascript追加添加href

使用javascript追加添加href,javascript,jquery,Javascript,Jquery,也许对某些人来说是最基本的,但这让我发疯了!我不确定我做错了什么-我不能/不允许(?)附加一个href-只是根本没有得到处理 $('#lastViewed').append('<a href="/Path/To?_q="' + string + '>'); $('#lastViewed').append(.....some other stuffs.....); $('#lastViewed').append('</a>'); $('lastViewed')。追加(''

也许对某些人来说是最基本的,但这让我发疯了!我不确定我做错了什么-我不能/不允许(?)附加一个href-只是根本没有得到处理

$('#lastViewed').append('<a href="/Path/To?_q="' + string + '>');
$('#lastViewed').append(.....some other stuffs.....);
$('#lastViewed').append('</a>');
$('lastViewed')。追加('';
我正试着用一根绳子把“其他东西”包起来 谢谢

编辑
完整行:

$('#lastViewed').append('<div id="id_' + x + '<a href="/PVProduct/ProductDetail?_productID=' + JSON.parse(localStorage.getItem("pid_" + x)).productID);

$(“#lastViewed”).append(“这是不正确的。你不能像那样打开和关闭标签。相反,
append()
整个标签。或者更好的是,创建
a
元素和
append()
它:

$a = $('<a>').attr('href', yourHref).html(yourText);
$('#lastViewed').append($a);
$a=$('').attr('href',yourref).html(yourText);
$('#lastViewed')。追加($a);
您可以使用它

var link=document.createElement("a");
link.id="idName";
link.setAttribute("href", "your link");
document.appendChild(link);

什么是
string
?另外,您过早地关闭了
$('#lastview')。追加('')
可能会起作用-我可以按输入错误关闭吗?另外,请缓存您的选择器。
“感谢您为我指明了使用append关键字的正确方法。我最终生成了一个字符串并将其插入标记中。非常感谢!很高兴我能提供帮助。记住,jQuery也有
appendTo()