简体   繁体   English

试图在href标记中转义引号

[英]Trying to escape quotes in a href tag

I have the following line of code, 我有以下代码行,

$("#busdata").append("<div><a href="url">" + data.response.results[i].webUrl + "</a></div>");

I essentially want the "data.response.results[i].webUrl" to replace the url string, but I'm not quite sure how to escape the quotes properly. 我基本上希望“data.response.results [i] .webUrl”替换url字符串,但我不太确定如何正确地转义引号。

You can escape quotes by replacing them with \\" or just use single quotes - ' 你可以通过用\\"替换它们来引用它们,或者只使用单引号 - '

So "<div><a href="url">" becomes "<div><a href=\\"url\\">" or "<div><a href='url'>" 所以"<div><a href="url">"变成"<div><a href=\\"url\\">""<div><a href='url'>"

单引号'和字符串连接符+

$("#busdata").append("<div><a href='"+ data.response.results[i].webUrl +"'>" + data.response.results[i].webUrl + "</a></div>");

Your syntax is wrong. 你的语法错了。 You need to escape quotes. 你需要逃避报价。 Change your <a href="url"> to <a href=\\"url\\"> like this: 将您的<a href="url">更改为<a href=\\"url\\">如下所示:

$("#busdata").append("<div><a href=\"url\">" + data.response.results[i].webUrl + "</a></div>");

Or if you feel that's a bit tough, you can exchange the quotes, ' for " : 或者,如果你觉得有点难,你可以交换引号' for "

$("#busdata").append('<div><a href="url">' + data.response.results[i].webUrl + "</a></div>");

Else, if you are trying to add the URL from the response: 否则,如果您尝试添加响应中的URL:

$("#busdata").append("<div><a href=\"" + data.response.results[i].webUrl + "\">" + data.response.results[i].webUrl + "</a></div>");

if url is a variable $("#busdata").append("<div><a href='" + url +"'>" + data.response.results[i].webUrl + "</a></div>"); 如果url是变量$("#busdata").append("<div><a href='" + url +"'>" + data.response.results[i].webUrl + "</a></div>");

and if you want to write by yourself 如果你想自己写

$("#busdata").append("<div><a href='url'>" + data.response.results[i].webUrl + "</a></div>");

You can store it in variable instead : 您可以将其存储在变量中:

var url = data.response.results[i].webUrl;
$("#busdata").append("<div><a href='"+url+'">" + url + "</a></div>");

Hope this helps. 希望这可以帮助。

Simply do it following my example: 只需按照我的示例执行操作:

 var a = $('<a />', { href: 'url here', text: 'text here' }); $('body').append(a); 

You could do this : 你可以这样做:

 $("#busdata").append("<div><a href='"+data.response.results[i].webUrl +"'>" + data.response.results[i].webUrl + "</a></div>");

Since you are using double quotes for the string to append, you can use single quotes around the variable in the href attribute and then add that variable. 由于您要使用双引号来追加字符串,因此可以在href属性中的变量周围使用单引号,然后添加该变量。

This is most easily achieved by not building HTML by smashing strings together in the first place. 通过首先将字符串粉碎在一起来构建HTML,最容易实现这一点。

$("#busdata").append(
    $("<div />").append(
        $("<a />").attr("href", data.response.results[i].webUrl)
    )
);

Escaping quotes is not necessary 不需要转义引号

$("#busdata")
.append("<div><a href=" 
  + data.response.results[i].webUrl 
  + ">" 
  + data.response.results[i].webUrl 
  + "</a></div>"
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM