链接的代码如下:
<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 :(得分: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');
}
另一个有趣的想法:改变它调用一个立即修复链接的函数,然后触发点击:
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();
}
答案 1 :(得分: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;
}
}
另一方面,如果你想删除确认框并改变:
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属性。
答案 2 :(得分: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"