1

链接的代码如下:

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

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

4

3 回答 3

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/

于 2013-03-28T04:11:24.307 回答
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 属性。

于 2013-03-28T03:50:50.787 回答
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"

于 2013-03-28T03:54:23.210 回答