首页 > 解决方案 > 使用 jquery 创建链接标签并连接 href 属性以存储结果变量时遇到问题

问题描述

为此,我创建了一个名为link的变量来存储链接的信息,如下所示:

var link = '<li>' + '<a href="vote.php?id=" + result + '>' + '<b>' *text goes here* + '</b>' + </a>' + '</li>'; $("#lists").append(link);

在链接的a标签中,href 是存储一个名为id = 一个名为result的变量的数据。但是当我单击链接时,它不会显示在地址选项卡中,它只显示vote.php?id=而不显示结果变量。那么请问我该如何处理这个问题?

请注意,在链接的 href 中,变量result连接到id

标签: javascriptjquery

解决方案


你搞砸了你的'and ",所以在错误的地方打破/加入字符串

'<a href="vote.php?id=" + result + '>'   // Wrong
'<a href="vote.php?id=' + result + '">'  // Correct

let result = 'demo';
let placeholder = 'text goes here';

let link = '<li>' + '<a href="vote.php?id=' + result + '">' + '<b>' + placeholder + '</b>' + '</a>' + '</li>';

$("#lists").append(link);
console.log($('a').attr('href'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="lists"></div>


推荐阅读