我有以下代码:
$('a').on('click', function () {
$('#show').html($(this).attr('data-owner') + '<br><img src="' + $(this).attr('data-owner_logo') + '"/><br>' + '<br>' + $(this).attr('data-owner_url'));
我已经修改了它,以便 data-owner_logo 返回 html div 中的图像,但是如何修改代码,以便 data-owner_url 返回一个可点击的链接作为指定的文本(例如“更多信息”)而不是完整网址?感谢任何帮助,我已经尝试了一切! 谢谢
最佳答案
也许 html()
不是在这里调用的最佳函数,但如果必须的话:
$('a').on('click', function () {
$('#show').html(... + '<a href="' + $(this).attr('data-owner_url')
+ '">More info</a>'
);
我更喜欢使用 append()
,它更具可读性(但速度较慢):
$('#show')
.empty() // clear node
.append(document.createTextNode($(this).attr('data-owner')))
.append($('<br />'))
.append($('<img />').attr('src', $(this).attr('data-owner_logo')))
.append($('<br />'))
.append($('<a />').attr('href', $(this).attr('data-owner_url')).text('More info'))
;
关于javascript - 如何替换 getelementbyid 的结果以显示文本链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31992624/