用Javascript替换字符串中的html

replace html in string with Javascript

本文关键字:html 字符串 Javascript 替换      更新时间:2023-09-26

我四处寻找,但没有找到答案。这是我的问题:

我从CKEDITOR中得到一个页面,其中包含:

var oldText = CKEDITOR.instances.message.getData();

到目前为止,一切都很好。字符串包含如下内容:

<table cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td align="left"
                style="padding-top: 24px; padding-right: 0px; padding-bottom: 0px; padding-left: 28px;"
                valign="top">
                <!--//logo//-->
                <a href="urlToSite" target="_blank"><img alt=""
                    src="urlToSite/image/data/Logo/logog7.png" /></a>
            <!--//end logo//-->
            </td>
            <td align="right"
                style="padding-top: 20px; padding-right: 28px; padding-bottom: 0px; padding-left: 0px;"
                valign="top">
                <div style="font-size: 18px; color: rgb(53, 115, 173);">Monday, 29
                    July 2013</div>
                <div style="font-size: 11px; color: rgb(53, 115, 173);">
                    <a
                        href="http://urlToSite/index.php?route=ne/subscribe&amp;uid={uid}&amp;key={key}"
                        style="text-decoration: none; color: rgb(53, 115, 173);">Subscribe</a>&nbsp;|&nbsp;<a
                        href="http://urlToSite/index.php?route=ne/unsubscribe&amp;uid={uid}&amp;key={key}"
                        style="text-decoration: none; color: rgb(53, 115, 173);">Unsubscribe</a>&nbsp;|&nbsp;<a
                        href="urlToSite"
                        style="text-decoration: none; color: rgb(53, 115, 173);">Visit our
                        site</a>
                </div>
            </td>
        </tr>
    </tbody>
</table>

现在我想把取消订阅链接改成这样吗(这发生在switch语句中):

http://urlToSite/index.php?route=ne/unsubscribe&amp;uid={uid}&amp;key={key}&c=1&l=12

(注意,l参数是动态的)。但我也不想再让他回到原来的网址。

我如何在字符串中搜索该URL(有没有这些额外的参数,这取决于)并替换它

我对此一无所知,但如果你们能帮助我,甚至为我指明一点正确的方向,那会很棒吗。

所以在某种程度上,这就是我想要实现的:

  • 我有这个URL:http://urlToSite/index.php?route=ne/unsubscribe&amp;uid={uid}&amp;key={key}
  • 我想将其更改为以下网址:http://urlToSite/index.php?route=ne/unsubscribe&amp;uid={uid}&amp;key={key}&c=1&i5
  • 那么我不想让url变成这样:http://urlToSite/index.php?route=ne/unsubscribe&amp;uid={uid}&amp;key={key}&c=1&i=2
  • 但我也希望无法将URL恢复正常(我也可以在静态变量中这样做,所以这没什么大不了的…)

-顺便说一句,抱歉英语不好;)

编辑

你们认为以下是解决这个问题的好方法吗???

1: I save the default URL in an string
2: I do an replace with the default URL on the oldText, and insert the new URL
3: I save the new URL in an difrent string
4: Next time i do an replace with the saved new URL on the oldText and insert an newer URL
5: and save that and so on...

编辑2我现在使用的代码如下(答案来自Woody)

var oldText = CKEDITOR.instances.message.getData();
var div = document.createElement("div");
div.innerHTML = oldText;
var a = div.querySelector('a[href^="http://www.gospel7.com/index.php?route=ne/unsubscribe"]');
$(a).attr("href", "http://urlToSite/index.php?route=ne/unsubscribe&amp;uid={uid}&amp;key={key}&c=1&l=12/")
console.log(a);

获取HTML字符串并将其放入内存中的div中,这样您就可以对其进行操作。

vardiv=document.create元素("div");

div.innerHTML = oldText;

现在您有了一个div,可以查询anchor标记。

 var a = div.querySelector("a[href^=<start of URL you want to change] ");

现在将锚点上的href设置为您想要的。完成后获取div的innerHTML。

var oldURL = "http://urlToSite/index.php?route=ne/subscribe&amp;uid={uid}&amp;key={key}";
var newURL = "http://urlToSite/index.php?route=ne/unsubscribe&amp;uid={uid}&amp;key={key}&c=1&l=12"
var newText = oldText.replace("oldURL", "newURL");

您只是在进行查找和替换(同时存储当前变量)吗?

如果您需要更动态的东西,请查看正则表达式http://www.regular-expressions.info/reference.html