使用 Jquery 在 Html URL 上创建超链接(img src 链接除外)
我试图在我的页面中的每个网址上创建一个超链接。 有这样的代码:
<div id="divC">
Hello testing message
My profile link : http://stackoverflow.com/users/568085/abhishek and
my user account link :
<a href="http://stackoverflow.com/users/568085/abhishek">
<img height="58" width="208" title="Stack Overflow profile for Abhishek at Stack Overflow, Q&A for professional and enthusiast programmers" alt="Stack Overflow profile for Abhishek at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/568085.png?theme=dark">
</a>
</div>
我使用下面的 javascript 函数在内容页面中的每个 URL 上添加链接:
<script>
//call function for linking every url
createLinks();
function createLinks()
{
var exp=/(((\b(https?|ftp|file):\/\/))[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
$("div#divC").each(function(index) {
$(this).html($(this).html().replace(exp, "<a target='_self' class='msg_links' href=$1>$1</a>"));
});
}
</script>
在上面的代码中工作正常,但我不想在 。 当我运行此命令时,它还在
,
如何避免在 src 中创建链接?
I am trying to make a hyperlink on every url in my page.
There is code like this:
<div id="divC">
Hello testing message
My profile link : http://stackoverflow.com/users/568085/abhishek and
my user account link :
<a href="http://stackoverflow.com/users/568085/abhishek">
<img height="58" width="208" title="Stack Overflow profile for Abhishek at Stack Overflow, Q&A for professional and enthusiast programmers" alt="Stack Overflow profile for Abhishek at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/568085.png?theme=dark">
</a>
</div>
and I used below javascript function to add link on every URL in content page :
<script>
//call function for linking every url
createLinks();
function createLinks()
{
var exp=/(((\b(https?|ftp|file):\/\/))[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
$("div#divC").each(function(index) {
$(this).html($(this).html().replace(exp, "<a target='_self' class='msg_links' href=$1>$1</a>"));
});
}
</script>
In above code working fine but I don't want to create link on <img src='www.test.com'>
.
when I run this it also created link in the <img src="<a href='www.test.com'>www.test.com</a>" >
,
How can I avoid create link in <img>
src.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊...我明白你在做什么。我不久前就这么做了,构建了一个插件来替换 smiley 的 :D。这段代码工作得更好吗?
Ah... I see what you are doing. I've did it a while ago, building a plugin that replaces smiley's :D. Does this code work better?
将 id 添加到您的标签中并将其与您的 jquery 匹配,
这是因为您正在使用 .each()。如果您想更改多个,请使用标签的类属性。
Put an id to you tag and match it with your jquery
It is because you are using .each(). If you want to change multiple use a class attribute for your tags.