![](/img/trans.png)
[英]How can I replace the text in an HTML tag but not in an href link?
[英]How can I replace the result of a getelementbyid to show a text link?
我有下面的代码:
$('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返回可点击链接作为指定文本(例如'More info')而不是完整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'))
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.