没有使用jQuery呈现href

蓝天

我写了下面的代码来使用javascript创建url href:

$(document).ready(function() {

var js = [{"title":"New ECB Boss Christine Lagarde Made A Serious Bitcoin Warning","link":"https://www.forbes.com/sites/billybambrough/2019/07/07/new-ecb-boss-christine-lagarde-made-a-serious-bitcoin-warning/","text":"Bitcoin and other crypto-assets have long divided traditional economists and bankers with some warning over their instability and others ...","img":"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcR-cf7C5AWRZ2mt1qip3sV9MzLok1tGn0CAInRO6kqP1J5b_VQQdQRmKXW8NsNi2BTxOYnSl-Q"},{"title":"Bitcoin Approaches $11500 as Top Cryptos See Gains","link":"https://cointelegraph.com/news/bitcoin-approaches-11-500-as-top-cryptos-see-gains","text":"Saturday, July 6 — most of the top 20 cryptocurrencies are reporting moderate gains on the day by press time, as Bitcoin (BTC) hovers just ...","img":"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQdGcmi26RdOCRqaQ0kQ8raWmw2uA7PHv3Oi936yUNA8IQN9OhXKS6Rar8gBF_7Jwd_GvRw2UA"}]

for (var i = 0; i < js.length; i++) {
    console.log('<div><a href='+js[i].link+'>'+js[i].title+'</a></div>')
    $('body').append('<div><a href='+js[i].link+'>'+js[i].title+'</a></div>')
/*   $('body').append('<div>'+js[i].text+'</div>') */
}

});

问题是:“新欧洲央行老板克里斯蒂娜·拉加德提出了严重的比特币警告”,而“比特币逼近11500美元,因为顶级加密货币见涨势”则被渲染为href

如何为两个js数组元素生成href?生成href的代码是相同的,因此js数组中是否存在导致这种不一致的值?

jsfiddle:https ://jsfiddle.net/adrianfiddleuser/jvq7ogk5/66/

一定的表现

问题是链接末尾的斜杠:

"link":"https://www.forbes.com/sites/billybambrough/2019/07/07/new-ecb-boss-christine-lagarde-made-a-serious-bitcoin-warning/"

导致

$('body').append('<div><a href=https://example.com/foo/>title</a></div>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

基本上,jQuery错误地将/>之前权利解释/>title</a>为自动关闭标签。即使在使用<a>s时,s也不是自动关闭的/>

<!-- Renders properly - the tag does not self-close: -->
<a href=https://example.com/>click</a>

将该属性用引号引起来,例如<a href="https://example.com/">title</a>

var js = [{
  "title": "New ECB Boss Christine Lagarde Made A Serious Bitcoin Warning",
  "link": "https://www.forbes.com/sites/billybambrough/2019/07/07/new-ecb-boss-christine-lagarde-made-a-serious-bitcoin-warning/",
  "text": "Bitcoin and other crypto-assets have long divided traditional economists and bankers with some warning over their instability and others ...",
  "img": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcR-cf7C5AWRZ2mt1qip3sV9MzLok1tGn0CAInRO6kqP1J5b_VQQdQRmKXW8NsNi2BTxOYnSl-Q"
}, {
  "title": "Bitcoin Approaches $11500 as Top Cryptos See Gains",
  "link": "https://cointelegraph.com/news/bitcoin-approaches-11-500-as-top-cryptos-see-gains",
  "text": "Saturday, July 6 — most of the top 20 cryptocurrencies are reporting moderate gains on the day by press time, as Bitcoin (BTC) hovers just ...",
  "img": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQdGcmi26RdOCRqaQ0kQ8raWmw2uA7PHv3Oi936yUNA8IQN9OhXKS6Rar8gBF_7Jwd_GvRw2UA"
}]

for (var i = 0; i < js.length; i++) {
  $('body').append('<div><a href="' + js[i].link + '">' + js[i].title + '</a></div>')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

或者,更优雅和安全,而不是写出来的HTML手动创建一个<a>明确,并分配给它的href属性和它的textContent

var js = [{
  "title": "New ECB Boss Christine Lagarde Made A Serious Bitcoin Warning",
  "link": "https://www.forbes.com/sites/billybambrough/2019/07/07/new-ecb-boss-christine-lagarde-made-a-serious-bitcoin-warning/",
  "text": "Bitcoin and other crypto-assets have long divided traditional economists and bankers with some warning over their instability and others ...",
  "img": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcR-cf7C5AWRZ2mt1qip3sV9MzLok1tGn0CAInRO6kqP1J5b_VQQdQRmKXW8NsNi2BTxOYnSl-Q"
}, {
  "title": "Bitcoin Approaches $11500 as Top Cryptos See Gains",
  "link": "https://cointelegraph.com/news/bitcoin-approaches-11-500-as-top-cryptos-see-gains",
  "text": "Saturday, July 6 — most of the top 20 cryptocurrencies are reporting moderate gains on the day by press time, as Bitcoin (BTC) hovers just ...",
  "img": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQdGcmi26RdOCRqaQ0kQ8raWmw2uA7PHv3Oi936yUNA8IQN9OhXKS6Rar8gBF_7Jwd_GvRw2UA"
}];

js.forEach(({ link, title }) => {
  $('<div><a></a></div>')
    .appendTo('body')
    .find('a')
    .prop('href', link)
    .text(title);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章