【发布时间】:2012-12-30 23:34:56
【问题描述】:
我有一个字段,用户可以在其中输入一些文本,包括链接等。保存数据并预览文本后,我需要将所有链接格式化为<a href> 上设置的某个内联样式标签。没有使用 CSS 的选项,所以它应该是内联的。
输入文本示例:Some text goes here. Please go to my web site, click here <a href="http://www.onewebsite.com" target="_blank">www.onewebsite.com</a>. Then some more text here…..with more links etc……
预览时的示例文本:Some text goes here. Please go to my web site, click here <a href="http://www.onewebsite.com" target="_blank" style="font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 12px; color: #fa860b;">www.onewebsite.com</a>. Then some more text here…..with more links etc……
请注意,style="font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 12px; color: #fa860b;" 现在已添加到 <a href>。
如何以简单的方式使用 JavaScript 来实现?这将是某种功能,例如:
function formatPreview(myText){
defaultStyle="style=\"font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 12px; color: #fa860b;\"";
myText = myText, find all a href and enter my defaultStyle
return myText
}
请告诉我。谢谢你的帮助。
【问题讨论】:
-
为什么需要 JavaScript?这可以通过 CSS 来实现。
-
嗨 - 一个非常重要的评论是 this 将在电子邮件中使用,这意味着样式应该是内联的。所以我无法访问 jQuery 和 CSS。所有 HREF 链接都需要添加 STYLE 标记,以便链接可以在所有电子邮件客户端中呈现。
标签: javascript regex hyperlink href inline-styles