Change #hash tag to link on page load

▼魔方 西西 提交于 2019-12-19 11:34:21

问题


How to change # tag + Character to link using javascript

This is going #right on the way

with

This is going <a href="http://twitter.com/#!/search/right">right</a> on the way

even this is also acceptable

This is going <a href="http://twitter.com/#!/search/right">#right</a> on the way

The page lots's of # (hash) tag in different classes and id's


回答1:


var string = "This is going #right on the way";
string.replace(/#(\S*)/g,'<a href="http://twitter.com/#!/search/$1">$1</a>')

Demo: http://jsfiddle.net/dKm82/




回答2:


here is my suggestion

(function(){

$.fn.hashlink = function(){
  this.text(this.text().replace(\#(w+)\),"<a href='twitter.com/#!/search/$1'>$1</a>")
  text = \#w+\g.
}    

})


来源:https://stackoverflow.com/questions/7575041/change-hash-tag-to-link-on-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!