javascript - 使用 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。
解决方案
你搞砸了你的'
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>
推荐阅读
- swift - 如何在 Swift Combine 中创建自定义链?
- python - 如何从 github 下载这些文件?
- javascript - onscroll事件javascript之前和之后的window.pageYOffset
- c++ - 用在别处创建的对象填充 std::vector
- amazon-web-services - 为什么我更换 AMI 后新创建的 EC2 实例没有向 AWS Cloud Formation 发送资源信号?
- python - 我需要帮助确定与 Selenium 一起使用的 Web 表单字段
- c# - 在 ImageResizer 中更改图像时的更改/编辑顺序
- django - 在 FilterSet 中组合过滤器参数
- android - InvocationTargetException 在适配器类中膨胀视图
- maven - jibx-maven-plugin 1.2.5 schema-codegen 目标
忽略值