0

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

我从 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(使用所有这些额外参数,或者不使用,这取决于)并替换它。

我对此一无所知,但是如果你们可以帮助我,或者甚至为我指出正确的方向,那将是非常棒的。

所以总的来说,这就是我想要实现的:

  • 我有这个网址: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
  • 然后我不能使网址像这样:http://urlToSite/index.php?route=ne/unsubscribe&amp;uid={uid}&amp;key={key}&c=1&i=2
  • 但我也无法将 URL 恢复正常(我也可以在静态变量中执行此操作,所以这没什么大不了的......)

-btw- 抱歉英语不好;)

编辑

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

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...

EDIT2 我现在使用的代码如下(答案来自伍迪

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);
4

2 回答 2

1

获取该 HTML 字符串并将其放入内存中的 div 中。所以你可以操纵它。

var div = document.create element("div");

div.innerHTML = oldText;

现在你有一个可以查询锚标记的 div。

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

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

于 2013-07-29T20:50:57.057 回答
0
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

于 2013-07-29T20:45:23.287 回答