使用Javascript在HTML代码中修改mso注释?

使用Javascript在HTML代码中修改mso注释?,javascript,jquery,html,comments,Javascript,Jquery,Html,Comments,我目前正在尝试开发一个小工具,用于更改html文件中的某些元素,其中一个元素是电子邮件的“防弹CSS按钮”,如下图所示: 注释后的代码块如下所示: <!--[if mso]><v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="http://google.com" style="height:30px;v-text-a

我目前正在尝试开发一个小工具,用于更改html文件中的某些元素,其中一个元素是电子邮件的“防弹CSS按钮”,如下图所示:

注释后的代码块如下所示:

<!--[if mso]><v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="http://google.com" style="height:30px;v-text-anchor:middle;width:170px;" arcsize="9%" stroke="f" fillcolor="#34adb2"><w:anchorlock><center></center></w:anchorlock></v:roundrect><![endif]-->
到目前为止,这只会提醒本机HTML注释,而不是我想更改的特定mso注释


有没有其他方法来针对这一评论?任何帮助都将不胜感激

我在不同的场景中编写了一些简单的代码,你可以使用其中一个

我用IE 11测试了它们

在这些示例中,我们有一些HREFs值:


www.mysite。com它在IE11和Chrome上工作。它在哪里不起作用?在Chrome中试用,奇怪的是它只输出本地HTML注释,如
注意
.contents()
将只返回
的子节点,它不会递归搜索,在您给出的示例中,注释位于
中。请发布您正在使用的HTML代码。感谢您的回答,在提供的代码更改了mso注释正确性中的href之后,当前按钮包含一条
注释,但它也修改了标记底部的注释-有没有办法避免这种情况?@BerB,我在回答中添加了一些不同场景的示例。你可以选择你想要的女巫。如果您有任何问题,请写在这里。您好,我想应用示例B-它工作正常,但我的HTML结构a有点不同,请按照此链接查看图像的屏幕截图:;如您所见,我有一个额外的
@BerB,重用新的示例B。。。我更改了replaceHrefWithNew函数,并添加了
IF
语句,以防止在没有任何
HREF
属性时替换
HREF
。另外,我在jquery代码的
每个
部分中添加了
IF
语句,以避免下一步的工作
如何更改MSO按钮的标题/标题?
$(function() {
    $("body").contents().filter(function(){
        return this.nodeType == 8;
    }).each(function(i, e){
        alert(e.nodeValue);
    });
});
<div id="mybtn">
    <a href="http://www.mySite. com" style="background-color:#556270;background-image:url(http://i.imgur.com/0xPEf.gif);border:1px solid #1e3650;border-radius:4px;color:#ffffff;display:inline-block;font-family:sans-serif;font-size:13px;font-weight:bold;line-height:40px;text-align:center;text-decoration:none;width:200px;-webkit-text-size-adjust:none;mso-hide:all;">Show me the button!</a>
    <!--[if mso]><v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="http://http://www.mySite. com" style="height:40px;v-text-anchor:middle;width:200px;" arcsize="10%" strokecolor="#1e3650" fill="t">
          <v:fill type="tile" src="http://i.imgur.com/0xPEf.gif" color="#556270" />
          <w:anchorlock/>
          <center style="color:#ffffff;font-family:sans-serif;font-size:13px;font-weight:bold;">Show me the button!</center>
     </v:roundrect><![endif]-->
</div>
<div id="mybtn">
    <!--[if mso]><v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="http://http://www.mySite. com" style="height:40px;v-text-anchor:middle;width:200px;" arcsize="10%" strokecolor="#1e3650" fill="t">
          <v:fill type="tile" src="http://i.imgur.com/0xPEf.gif" color="#556270" />
          <w:anchorlock/>
          <center style="color:#ffffff;font-family:sans-serif;font-size:13px;font-weight:bold;">Show me the button!</center>
     </v:roundrect><![endif]-->
    <a href="http://www.mySite. com" style="background-color:#556270;background-image:url(http://i.imgur.com/0xPEf.gif);border:1px solid #1e3650;border-radius:4px;color:#ffffff;display:inline-block;font-family:sans-serif;font-size:13px;font-weight:bold;line-height:40px;text-align:center;text-decoration:none;width:200px;-webkit-text-size-adjust:none;mso-hide:all;">Show me the button!</a>
</div>
$("div#mybtn").contents().filter(
    function(){
        return this.nodeType == 8;
        }).each(function(i,e){
            if(i==0)
            {
               $(this)[0].data = replaceHrefWithNew($(this)[0].data,"http://www.example.com");
               console.log($(this));
             }
        });

function replaceHrefWithNew(myMso,newHrefValue)
{
    var newMso=myMso;
    var indexOfStartHref=myMso.indexOf("href")+6;
    if(indexOfStartHref>=6)
    {
        var indexOfEndHref=myMso.indexOf("\"",indexOfStartHref);

        var part1=myMso.substring(0,indexOfStartHref);
        var part2=myMso.substring(indexOfStartHref,indexOfEndHref);
        var part3=myMso.substring(indexOfEndHref);

        newMso= part1 + newHrefValue + part3;
    }
    alert(newMso);
    return newMso;
}
$(document).ready(function(){
    var webAddress="http://www.example.com";
    $("div#mybtn").contents().filter(
    function(){
        return this.nodeType == 8;
        }).each(function(i,e){
            if(i==0)
            {
               $(this)[0].data = replaceHrefWithNew($(this)[0].data,webAddress);
               console.log($(this));
            }
        });
    $("div#mybtn").find('a:first').prop("href",webAddress);
});

function replaceHrefWithNew(myMso,newHrefValue)
{
    var newMso=myMso;
    var indexOfStartHref=myMso.indexOf("href")+6;
    if(indexOfStartHref>=6)
    {
        var indexOfEndHref=myMso.indexOf("\"",indexOfStartHref);

        var part1=myMso.substring(0,indexOfStartHref);
        var part2=myMso.substring(indexOfStartHref,indexOfEndHref);
        var part3=myMso.substring(indexOfEndHref);

        newMso= part1 + newHrefValue + part3;
    }
    alert(newMso);
    return newMso;
}
$(document).ready(function(){
    var webAddress="http://www.example.com";
    $("div#mybtn").find('a:first').prop("href",webAddress);
});