Javascript 无法使用jQuery动态添加超链接?
我想将Javascript 无法使用jQuery动态添加超链接?,javascript,jquery,Javascript,Jquery,我想将href添加到).html(I) ), $('') ), $(“”) ); } }); 您的代码中有错误的“,您在连接i之前关闭了href标记 $('<a href="https://www.my-domain.so/goto?link-id='+i+'"></a>').html(i) // moved to here--------^------------- $('').html(i) //搬到这
href
添加到).html(I)
),
$('')
),
$(“”)
);
}
});代码>
您的代码中有错误的“
,您在连接i
之前关闭了href
标记
$('<a href="https://www.my-domain.so/goto?link-id='+i+'"></a>').html(i)
// moved to here--------^-------------
$('').html(i)
//搬到这里--------^-------------
另外,您正在使用的$('')
和$('')
是另一个错误,它们不是有效的选择器或html创建者。我认为您正在尝试将标记括起来。实际上,无需使用它来获取更多信息。请访问:
检查更新的代码段
$(文档).ready(函数(){
变量链接=[{
“_id”:“57448A261F3E61616B34739F2”,
“索引”:0
}, {
“_id”:“57448a2663568bbfc9dafe74”,
“索引”:1
}, {
“_id”:“57448a26db2fbfa5cd38731f”,
“索引”:2
}, {
“_id”:“57448a2650e28fbe8e2d87b3”,
“索引”:3
}, {
“_id”:“57448a26a5e9ec8bd78c9809”,
“索引”:4
}, {
“_id”:“57448a26fa772d6314ddc059”,
“指数”:5
}, {
“_id”:“57448a26ba5a1782ae7c14af”,
“索引”:6
}];
for(链接中的变量i){
$('#链接列表')。追加(
$(“”{
html:$(''){
html:$(''){
href:“https://www.my-domain.so/goto?link-id=“+链接[i]。索引,
正文:i
})
})
})
);
}
});
试试这种代码的和平
for(链接中的变量i){
美元(“”)
.append($(“”)
.append($(“”).attr({“href”:https://www.my-domain.so/goto?link-id=“+links[i].\u id}).html(links[i].index)))
.附于(“#链接列表”);
}
我不知道为什么,但我很震惊,$(“它之所以有效,是因为浏览器充分利用了您提供的内容。”
for (var i in links) {
$("<li/>")
.append($("<span/>")
.append($("<a/>").attr({ "href": "https://www.my-domain.so/goto?link-id=" + links[i]._id }).html(links[i].index)))
.appendTo("#link-list");
}