2013-03-28 44 views
1

删除的JavaScript链接的代码如下:从链接

<td class="buttonColumn"> 
<a href="javascript:confirmDialog('Are you sure you want go?', 'http://google.com');" class="actionButtonNew">Leave to Google</a> 
</td> 

<td class="buttonColumn"> 
<a href="javascript:confirmDialog('Are you sure you want go?', 'http://yahoo.com');" class="actionButtonNew">Leave to Yahoo</a> 
</td> 

有在页面上需要相应的更改多个按钮具有完全相同的格式。我在想这样的事情:

var fix = document.getElementsByClassName('actionButtonNew'); 
for (var i=0; i<fix.length; i++) { 

我不知道如何完成代码。我试过这个:

var fix = document.getElementsByClassName('actionButtonNew'); 
for(var i = 0; i < fix.length; i++) { 
    try { 
     var l = fix[i]; 
     var h = fix[i].parentNode.getElementsByClassName('actionButtonNew')[0].href.replace("javascript:confirmDialog('Are you sure you want to go?',", ""); 
     l.href = h; 
    } catch(e) {} 
} 

我知道这是非常混乱和不好。它确实将链接更改为:'http://google.com');

所以我想我可能会走在正确的轨道上。请帮帮我。提前致谢!

+0

你想要在链接被更改后实现什么功能(例如,没有链接,删除所有http:// ...)? – 2013-03-28 03:44:10

+0

我想要链接指向网站。即:谷歌链接去http://google.com – MathMan08 2013-03-28 03:45:07

+0

那么删除确认? – 2013-03-28 03:45:32

回答

1

这里有一个想法:而不是处理了势必打破一些边缘的情况下,与不确认重定向功能取代confirmDialog凌乱的正则表达式:

window.noConfirm = function(m, url) { 
    document.location.href = url; 
} 

var fix = document.getElementsByClassName('actionButtonNew'); 
for (var i = 0; i < fix.length; i++) { 
    fix[i].href = fix[i].href.replace('confirmDialog', 'noConfirm'); 
} 

http://jsfiddle.net/AV4tB/

另一个有趣的想法:改变它称为立即修复链接的功能,然后触发点击:

window.fixMe = function(m, url) { 
    this.href = url; 
} 

var linksToFix = document.getElementsByClassName('actionButtonNew'); 
for (var i = 0; i < linksToFix.length; i++) { 
    window.a = linksToFix[i]; 
    linksToFix[i].href = linksToFix[i].href.replace('confirmDialog(', 'fixMe.call(a, '); 
    linksToFix[i].click(); 
} 

http://jsfiddle.net/AV4tB/1/

+0

你在第一个上忘了分号,但是工作很棒! – MathMan08 2013-03-28 04:24:11

+0

嘿,没有想到这一点。太好了! – 2013-03-28 04:40:31

0

很烦人地对用户说“你真的想这么做吗?”每次点击一个链接。毕竟,后退按钮通常会将它们直接返回,否则如果快速退出键将取消导航。

由于A元素可能不是所有的链接(有些是目标),你可以在使用document.links集合页面中的所有链接,然后遍历是:

// Function to call 
function navPrompt() { 
    var text = this.textContent || this.innerText 
    return confirm('Are you sure you want to go to ' + text + '?'); 
} 

// Attach to all listeners 
window.onload = function() { 

    var links = document.links; 
    var i = links.length; 
    while (i--) { 
    links[i].onclick = navPrompt; 
    } 
} 

在otherhand,如果你要删除的comfirm箱和改变:

href="javascript:confirmDialog('Are you sure you want go?', 'http://google.com');" 

到:

href="http://google.com" 

你可以这样做:

window.onload = function() { 

    var link, links = document.links; 
    var i = links.length; 
    while (i--) { 
    link = links[i]; 
    link.href = link.href.replace(/.*(http[^\'\"]+).+/i,'$1'); 
    } 
} 

这将删除URL并将其分配给href属性。

+0

你可以做一个jsFiddle链接吗?我不能得到它的工作:/ – MathMan08 2013-03-28 03:57:31

+0

好工作:DI将投票当我可以:) – MathMan08 2013-03-28 04:25:10

0

试试这个:

var fix = document.getElementsByClassName('actionButtonNew'); 
for (var i = 0; i < fix.length; i++) { 
    fix[i].href = fix[i].href.replace(/javascript:confirmDialog\(\'Are you sure you want go\?\'\, \'(http\:\/\/.*\.(.){2,3})'\);/gi,'$1'); 
} 

其中使用正则表达式只用链接替换确认JS。
例如"javascript:confirmDialog('Are you sure you want go?', 'http://yahoo.com');"成为"http://yahoo.com"

+0

你的方式也很好,谢谢! – MathMan08 2013-03-28 04:24:35