我如何使用javascript将jquery数据属性中的链接替换为哈希标签 [英] How do I use javascript to replace hash tags with links from a jquery data-attribute

查看:109
本文介绍了我如何使用javascript将jquery数据属性中的链接替换为哈希标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在有效地尝试执行与此问题相同的操作 更改#hash标记以在页面加载时链接 但是我认为,因为我是从jquery对象的data-caption属性获取字符串的,所以无法正常工作.我有一个像这样的DOM元素

I'm effectively trying to do the same as in this question Change #hash tag to link on page load But I think because I'm getting the string from a jquery objects data-caption attribute something isn't working correctly. I have a DOM element like this

<a class="thumbnail" rel="gallery" data-caption="#drink from somewhere #fun" data-pid="36" href="http://domain.com/something"><img src="http://domain.com/somephoto.png" alt="acee7bd0d8339b9ddcf4a259ec7ddeec"></a>

基本上是一个缩略图,已加载到模式中,然后我试图获取属性标题并从任何哈希标签中创建链接

It's basically a thumbnail that is loaded into a modal then i'm trying to grab the attribute caption and create links out of any hash tags

 var caption = anchor.attr('data-caption') ? anchor.attr('data-caption') : null;
 console.log(caption);

其中变量锚是代表链接的jquery对象

where the variable anchor is a jquery object representative of the link

如果我查看日志中显示"#drink from #fun"的日志,就会看到标题

I can see there is a caption if i check the log it prints "#drink from somewhere #fun"

所以现在把它扔到正则表达式中替换fn

So now throwing that in a regex replace fn

caption.replace(/#(\S*)/g,'<a href="http://twitter.com/#!/search/$1">$1</a>');

然后将标题添加到具有活动链接的DOM.

Then append the caption to the DOM with the links active.

但是标题字符串什么也没发生,我得到的东西与我输入的东西一样.

But nothing happens to the caption string I just get the same thing out that I put in.

**编辑答案 愚蠢的错误忘记将返回值分配给变量

**EDIT ANSWER dumb mistake forgot to assign the return value to a variable

var captionLinks = caption.replace(/#(\S*)/g,'<a href="http://twitter.com/#!/search/$1">$1</a>');

推荐答案

如果您的问题中的代码发布与使用时完全相同,则需要分配caption.replace()的结果.只是调用.replace()不会更改caption.

If you code in your question is posted exactly as you use it, you need to assign the result of the caption.replace(). Just calling .replace() won't change caption.

假设您正在做的是这样:

Assuming all you are doing is this:

caption.replace(/#(\S*)/g,'<a href="http://twitter.com/#!/search/$1">$1</a>');

尝试像这样使用它:

caption = caption.replace(/#(\S*)/g,'<a href="http://twitter.com/#!/search/$1">$1</a>');

根据 文档 Replace:

返回一个新的字符串,其中部分或全部匹配的模式替换为 一个替代品.该模式可以是字符串或RegExp,并且 替换可以是字符串,也可以是每次匹配都要调用的函数.

Returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.

当然,如果您已经这样做了,而您根本没有发布,那是其他问题了.

Off course, if you already have been doing it and you simply didn't post that than the issue is something else.

如果是这种情况,请告诉我,我将删除答案,因为它显然不适用.

Let me know if that is the case and I will remove my answer as it then obviously does not apply.

这篇关于我如何使用javascript将jquery数据属性中的链接替换为哈希标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆