我写了一些各种场景的简单代码,你可以使用其中一个你想要的基础女巫。 ;)
我通过 IE 11
对它们进行了测试
在这些示例中,我们有一些 HREF 值:
www.mysite。 com 默认值
www.example.com 新价值
现在代码:
HTML
这个:
<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>
运行任何脚本之前的代码状态:
.
.
A) Javascript(只是改变 v:roundrect 标签的 HREF 属性)
$("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;
}
运行脚本 A 后的结果
.
.
B) Javascript(同时更改v:roundrect标签和A标签的HREF属性)
$(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;
}
运行脚本 B 后的结果
.
.
C) Javascript(只需更改 HREF 属性 A 标签)
在这种情况下,您可以简单地使用下面的 jquery 代码:
$(document).ready(function(){
var webAddress="http://www.example.com";
$("div#mybtn").find('a:first').prop("href",webAddress);
});
运行脚本 C 后的结果