Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在“属性事件”中获取onclick“attribute event”返回值?_Javascript_Jquery_Javascript Events - Fatal编程技术网

Javascript 如何在“属性事件”中获取onclick“attribute event”返回值?

Javascript 如何在“属性事件”中获取onclick“attribute event”返回值?,javascript,jquery,javascript-events,Javascript,Jquery,Javascript Events,考虑以下代码示例: <a href="some url" onclick="confirm('ok to proceed ?')">bla</a>` <script type="text/javascript"> $(document).ready(function() { $("a").live("click", function (event) { // In that func

考虑以下代码示例:

<a href="some url" onclick="confirm('ok to proceed ?')">bla</a>`

<script type="text/javascript">
    $(document).ready(function() {
              $("a").live("click", function (event) {
                    // In that function, I want to get 
                    // the "confirm" return value                  
               });
</script>
可以在不修改DOM的情况下获取此返回值


谢谢………

您需要明确添加return一词,如下所示:

onclick="return confirm('ok to proceed?')"
$(document).ready(function() {
    // The removeAttr removes the original "onclick" attribute
    $("a").removeAttr('onclick').live("click", function (event) {
       var ret = confirm('ok to proceed ?');
       // Do what you want here because `ret` has the return 
       // value from the `confirm` call.
    });
});
编辑:我最初的回答是BS,以下是我最后的工作内容

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="jquery-1.3.2.js"></script>
    <script type="text/javascript">
$(document).ready(function(){
    $("a").click(function() {
        var stuff = confirm('ok?');
        if (stuff) {
        alert('was ok, stuff=' + stuff);
        }
    else {
        alert('was not ok, stuff=' + stuff);
        }
    });
});
    </script>
  </head>
  <body>
    <a href="http://jquery.com/">jQuery</a>
  </body>
</html>

您需要显式添加单词return,如下所示:

onclick="return confirm('ok to proceed?')"
$(document).ready(function() {
    // The removeAttr removes the original "onclick" attribute
    $("a").removeAttr('onclick').live("click", function (event) {
       var ret = confirm('ok to proceed ?');
       // Do what you want here because `ret` has the return 
       // value from the `confirm` call.
    });
});
编辑:我最初的回答是BS,以下是我最后的工作内容

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="jquery-1.3.2.js"></script>
    <script type="text/javascript">
$(document).ready(function(){
    $("a").click(function() {
        var stuff = confirm('ok?');
        if (stuff) {
        alert('was ok, stuff=' + stuff);
        }
    else {
        alert('was not ok, stuff=' + stuff);
        }
    });
});
    </script>
  </head>
  <body>
    <a href="http://jquery.com/">jQuery</a>
  </body>
</html>
如果我没记错的话,那应该行得通。然后,您可以访问jQuery块中的myVar,就像访问任何其他Javascript变量一样


如果我没记错的话,那应该行得通。然后,您可以像访问任何其他Javascript变量一样访问jQuery块中的myVar。

否,该值不会存储在任何地方供您访问。获取该值的唯一方法是将其移动到jQuery处理的单击事件,如下所示:

onclick="return confirm('ok to proceed?')"
$(document).ready(function() {
    // The removeAttr removes the original "onclick" attribute
    $("a").removeAttr('onclick').live("click", function (event) {
       var ret = confirm('ok to proceed ?');
       // Do what you want here because `ret` has the return 
       // value from the `confirm` call.
    });
});

否,该值未存储在任何可供您访问的位置。获取该值的唯一方法是将其移动到jQuery处理的单击事件,如下所示:

onclick="return confirm('ok to proceed?')"
$(document).ready(function() {
    // The removeAttr removes the original "onclick" attribute
    $("a").removeAttr('onclick').live("click", function (event) {
       var ret = confirm('ok to proceed ?');
       // Do what you want here because `ret` has the return 
       // value from the `confirm` call.
    });
});
getValueConfirm`

var isConfirm=false; 函数确认对话框{ isconfirm=确认“是否继续?”; } $document.readyfunction{ $a.liveclick,函数事件{ AlertisConfig;//您希望获取的值 }; }; getValueConfirm`

var isConfirm=false; 函数确认对话框{ isconfirm=确认“是否继续?”; } $document.readyfunction{ $a.liveclick,函数事件{ AlertisConfig;//您希望获取的值 }; };
嗯,也许也是这样。返回值存储在哪里?传递给匿名函数的jQuery事件参数?iirc如果用户单击cancel,事件将被取消,因此我不确定jQuery代码是否会执行。嗯,可能也是这样。返回值存储在哪里?传递给匿名函数的jQuery事件参数?iirc如果用户单击“取消”,事件将被取消,因此我不确定jQuery代码是否会执行。由于下面的答案,但我的问题恰恰是将属性event onclick和属性event onclick结合起来,无需明确修改onclick属性:请参阅我的jquery插件:>>>>>>>>如果onclick属性返回false,我想停止事件传播…感谢下面的答案,但我的问题恰恰是将属性event onclick和属性event onclick结合起来,不显式修改onclick属性:请参阅我的jquery插件:>>>>>>>如果onclick属性返回false,我想停止事件传播。。。