js:请用5个双引号说明该变量值 [英] js: please explain this variable value with 5 double quotations
问题描述
请简要看一下下面的代码:
Please, take a brief look at the code below:
<script type="text/javascript">
function recentpostslist(json) {
document.write('<ul class="recommended">');
var i;
var j;
for (i = 0; i < json.feed.entry.length; i++)
{
for (j = 0; j < json.feed.entry[i].link.length; j++) {
if (json.feed.entry[i].link[j].rel == 'alternate') {
break;
}
}
var postUrl = "'" + json.feed.entry[i].link[j].href + "'";//bs
var postTitle = json.feed.entry[i].title.$t;
var item = "<li>" + "<a href="+ postUrl + '" target="_blank">' + postTitle + "</a> </li>";
document.write(item);
}
document.write('</ul>');
}
</script>
<script src="https://xxxxxxxxxx.blogspot.com/feeds/posts/summary/-/recommended?max-results=3&alt=json-in-script&callback=recentpostslist"></script>
背景信息
我在Internet上的某个地方找到了 昨天,并且一直在尝试对其进行一些修改。
Background info
I found this somewhere on the Internet yesterday, and have been trying to modify it a bit, since.
变量 i
和 j
最初是在 for
循环中声明的,例如...
Variables i
and j
were originally declared within the for
loop, as in...
for (var i = 0; i < json.feed.entry.length; i++)
{
for (var j = 0; j < json.feed.entry[i].link.length; j++) {
if (json.feed.entry[i].link[j].rel == 'alternate') {
break;
}
}
...
...但是一个在(初学者)读了 w3schools.com 后,我得到了他们建议将变量声明保留在 for
循环之外。我的编程技能最好的时候还很少,所以说实话,我不知道我的印象是否正确。
...but after doing a bit of (beginners') reading on w3schools.com, I got the impression that they're recommending keeping the variables declaration outside the for
loop. My programming skills are scant, at their very best, so truth be told, I have no idea whether my impression was right.
然后我为<$开头的c $ c>< ul> 元素(第三行)。
Then I assigned a class to the <ul>
element at the beginning (3rd line).
到目前为止,很好。我检查了一下,脚本仍然有效。
So far, so good. I checked, and the script was still working.
它的作用是列出博客的3篇最新文章的标题,这些文章被标记为推荐。
What it does is list the titles of a blog's 3 latest posts that have been labeled "recommended".
然后我尝试将一个类分配给声明为...的< li>
元素。 。第15行的 item
变量的部分值(我理解吗?):
Then I made an attempt to assign a class to the <li>
element that's declared as a... ehm... part of the value (have I understood that right?) of the item
variable on line 15:
var item = "<li>" + "<a href="+ postUrl + '" target="_blank">' + postTitle + "</a> </li>";
但这似乎使代码无效,这不足为奇,
But that seems to invalidate the code, which came as no surprise, really, since it did seem like a bit of a long shot.
近距离拍摄时确实如此看看这行代码,但是,我必须说我对所有引号(单引号和双引号)都感到困惑。
When taking a closer look at this line of code, however, I must say I was quite baffled by all the quotation marks (single and double).
如果有人可以说明
< a href = + postUrl +' target = _ blank>'
为什么总共有5条双引号?
' target = _ blank>'
具有什么功能?
It would be greatly appreciated if someone could explain
"<a href="+ postUrl + '" target="_blank">'
Why are there 5 double quotations in total? Is it incorrect?
What function does '" target="_blank">'
serve?
推荐答案
它只是使用单引号来避免必须在双引号字符串中转义字面双引号。
It's just using single quotes to avoid having to escape the literal double quotes in a double-quoted string.
'" target="_blank"'
vs
"\"target=\"_blank\""
由于 postUrl
的定义包括该属性的引号,因此您的 item
中有一个额外的双引号,但是,我建议不将这些引号放在 postUrl
内,因为引号不是URL的部分。
Since the definition of postUrl
includes the quotes for the attribute, your definition of item
has an extra double quote in it. However, I would recommend not putting those quotes postUrl
, because the quotes are not part of the URL.
如果这样做,该行前面会缺少双引号。您有
If you do that, there is a missing double quote earlier in the line. You have
var item = "<li>" + "<a href=" + postUrl + '" target="_blank">' + postTitle + "</a> </li>";
,您应该拥有
var item = "<li>" + '<a href="' + postUrl + '" target="_blank">' + postTitle + "</a> </li>";
# ^^^^^^^^^^^
# Include a literal " after the =,
$ b $之后包含一个文字 b
(否则,目标
之前的引号是多余的,需要删除。)
(Otherwise, the quote preceding target
would be extraneous and need to be removed.)
该行会更清晰一些
var item = '<li> <a href="'+ postUrl + '" target="_blank">' + postTitle + "</a> </li>";
您可以责怪JavaScript没有内置的,方便的将值插值到字符串的方法。(尽管,请参阅如何在JavaScript中进行字符串插值?,这表明已添加。请注意,我不是Javascript程序员。
and you can blame JavaScript for not having a built-in, convenient way to interpolate values into a string. (Although, see How can I do string interpolation in JavaScript?, which suggests that this has been added. Note, I am not a Javascript programmer.
var item = `<li> <a href="${postUrl}" target="_blank">${postTitle}</a> </li>`;
)
这篇关于js:请用5个双引号说明该变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!