0

I have a text that must be send as an email. But to give the mail its layout we need to add some style. What we must do is the following: 1 find in a text one or more links with this format

<a href="RANDOM URL HERE">RANDOM TEXT</a>

2 change the links into this format:

<a href="RANDOM URL HERE" style="color: #FFFFFF; text-decoration: none;"><span style="color: #FFFFFF;">RANDOM TEXT</span></a> 

I have no idea how i can best do this, with regex or with DOM,...

Many thanks!

4

2 回答 2

1

首先,您应该考虑永远不要使用正则表达式来处理 HTML/XML 标记。您可以在此处阅读原因:https ://stackoverflow.com/a/1732454/1249581 :)

然后,为了回答这个问题,您应该执行以下操作:

var div = document.createElement("div"),
    links;

div.innerHTML = textOfYourEmail;
links = div.getElementsByTagName("a");

for (var i = links.length; i--;) {
    var link = links[i],
        span = document.createElement("span");

    link.style.textDecoration = "none";
    link.style.color = "#fff";
    span.style.color = "#fff";

    while (link.firstChild) {
        span.appendChild(link.firstChild);
    }
    link.appendChild(span);
}

textOfYourEmail = div.innerHTML;

它使用嵌入式方法进行 DOM 操作。它快速可靠。

于 2013-04-19T09:34:10.310 回答
0

在我看来,最好的方法是创建一个 CSS 类并将其添加到您的链接中。您不需要添加跨度标签!使用 DOM 是最好的方法。

于 2013-04-19T09:29:01.753 回答