[英]Make word in into a link jQuery
我正在努力做到这一点:
<td class="Monthly Status-cell">/fileid=XXXX</td>
展示为
http://www.domain.com/fileid=XXXX
您能告诉我代码有什么问题吗?
$('.Status-cell').replaceWith(function() {
var url = $.trim($(this).text());
return '<a href="' + url + '" target="_blank">' + url + '</a>';
});
谢谢!
不要将链接替换为td
,而应将链接放在td
。 您也没有将域添加到链接
$('.Status-cell').html(function() {
var url = window.location.protocol+'//'+window.location.host+$.trim($(this).text());
return '<a href="' + url + '" target="_blank">' + url + '</a>';
});
使用.html()
而不是.replaceWith()
将链接放在单元格中:
var domain = window.location.hostname; // current domain
$('.Status-cell').html(function(i, value) {
var link = domain + $.trim(value);
return '<a href="//' + link + '">' + link + '</a>';
});
尝试
$('.Status-cell').html(function(idx, value){
value = $.trim(value);
return '<a href="http://www.domain.com' + value + '">http://www.domain.com' + value + '</a>';
})
演示: 小提琴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.