在提供的字符串中设置锚标记
我想在提供的字符串中设置锚标记。它在 http:// 之后在 javascript 中进行搜索,并将其设置为锚标记。
function createLink(text, node){//text is the provided string
var start = text.indexOf('http://');
var end = text.indexOf(' ', start) + 1 || text.length - 1; //provides me with the wrong index
var link = text.substring(start, end);
var newLink = document.createElement('a');
newLink.href = link;
newLink.className = 'link';
newLink.target = '_blank';
newLink.innerHTML = link.substr(0, 20);
if(link.length >= 20){
$(newLink).append('...');
}
var head = text.substring(0, start);
var tail = text.substring(end);
node.innerHTML = '';
$(node).append(head).append(newLink).append(tail);
}
I want to set a anchor tag in a provided string. It search in javascript after http:// and make it to an anchor tag.
function createLink(text, node){//text is the provided string
var start = text.indexOf('http://');
var end = text.indexOf(' ', start) + 1 || text.length - 1; //provides me with the wrong index
var link = text.substring(start, end);
var newLink = document.createElement('a');
newLink.href = link;
newLink.className = 'link';
newLink.target = '_blank';
newLink.innerHTML = link.substr(0, 20);
if(link.length >= 20){
$(newLink).append('...');
}
var head = text.substring(0, start);
var tail = text.substring(end);
node.innerHTML = '';
$(node).append(head).append(newLink).append(tail);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您落后了 1:
http://jsfiddle.net/hunter/vNrrR/
I think you were off by 1:
http://jsfiddle.net/hunter/vNrrR/