1
4

6 回答 6

4

采用

return false;

在你的函数 someFunction() 中触发 onclick 事件。

于 2012-09-25T05:44:03.570 回答
3
function someFunction() {
  return confirm("Do you agree?");
}

确认函数返回用户的答案,而 someFunction 将其返回给<a>'s onclick。如果它为 false,它将停止链接到 href。

于 2012-09-25T05:44:35.280 回答
1

或使用 preventDefault:

function someFunction(e) {
   if (!confirm('.. ')) e.preventDefault();

  // continue download
}
于 2012-09-25T05:47:21.093 回答
0

您可以将 false 返回到 onclick 事件。

function someFunction()
{
    if(confirm("Do you want to download now ?")) {
        return true;
    }
    else
    {
        return false;
    }
}

<a href="http://www.google.com" onclick="return someFunction()">Click here</a>
于 2012-09-25T05:48:39.617 回答
0
function someFunction() {
 if(confirm("Do you want to download now ?") ) {
     // download file code goes here
 } else {
   return false;
 }
} 
于 2012-09-25T05:48:40.947 回答
0
<a href="" onclick="return someFunction();">Test</a>

function someFunction() {
  if (confirm("Do you agree?")) {
    /* some code if YES */
    return true;
  }
  else {
    /* some code if NO */
    return false;
  }
}

或者:

<a href="" onclick="someFunction(event);">Test</a>

function someFunction(event) {
  if (confirm("Do you agree?")) {
    /* some code if YES */
  }
  else {
    /* some code if NO */
    if (event.preventDefault) event.preventDefault();
    else event.returnValue=false;
  }
}
于 2012-09-25T05:59:15.520 回答