我正在尝试像在这个问题中一样 改变#hash标签链接页面加载 但是我认为因为我从jquery对象获取字符串data-caption属性有些东西不能正常工作.我有这样的DOM元素
<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>
Run Code Online (Sandbox Code Playgroud)
它基本上是一个加载到模态的缩略图然后我试图抓住属性标题并从任何哈希标签创建链接
var caption = anchor.attr('data-caption') ? anchor.attr('data-caption') : null;
console.log(caption);
Run Code Online (Sandbox Code Playgroud)
其中变量锚是表示链接的jquery对象
我可以看到有一个标题,如果我检查它打印的日志"#drink from somewhere #fun"
所以现在把它扔进一个正则表达式替换fn
caption.replace(/#(\S*)/g,'<a href="http://twitter.com/#!/search/$1">$1</a>');
Run Code Online (Sandbox Code Playgroud)
然后将标题附加到DOM并激活链接.
但是字幕字符串没有任何反应我只是得到了我输入的相同的东西.
**编辑答案愚蠢的错误忘记将返回值分配给变量
var captionLinks = caption.replace(/#(\S*)/g,'<a href="http://twitter.com/#!/search/$1">$1</a>');
Run Code Online (Sandbox Code Playgroud)