Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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事件,并在执行某些代码后恢复onclick_Javascript_Jquery - Fatal编程技术网

Javascript 绕过onclick事件,并在执行某些代码后恢复onclick

Javascript 绕过onclick事件,并在执行某些代码后恢复onclick,javascript,jquery,Javascript,Jquery,我有下面的html按钮,它有onclick事件 <button onclick="alert('button');" type="button">Button</button> 在jQuery/Javascript执行了一些js代码之后,我想继续使用按钮onclick处理程序,例如:jQuery alert first和than button alert 我尝试了很多方法,比如“删除attr并在执行代码后追加它,然后触发单击(它卡在循环中,我们知道原因:)”和“关闭”单

我有下面的html按钮,它有onclick事件

<button onclick="alert('button');" type="button">Button</button>
在jQuery/Javascript执行了一些js代码之后,我想继续使用按钮onclick处理程序,例如:jQuery alert first和than button alert

我尝试了很多方法,比如“删除attr并在执行代码后追加它,然后触发单击(它卡在循环中,我们知道原因:)”和“关闭”单击。但是没有运气

可以通过jQuery/javascript实现吗

非常感谢您的任何建议


谢谢

创建一个单独的javascript函数,其中包含单击按钮时要执行的操作(即删除onclick属性并在其自身函数中添加替换代码)

然后在函数末尾调用该函数

$('button').on('click', function(){
    alert('jquery');
});
所以你会留下这样的东西

function buttonFunction()
{
  //Do stuff here
}

$('button').on('click', function()
{
  alert('jquery');
  buttonFunction();
});

<button type="button">Button</button>
函数按钮函数()
{
//在这里做事
}
$('button')。在('click',function()上
{
警报('jquery');
按钮功能();
});
按钮
有点棘手

虽然有更好的方法传递参数。您可以使用数据属性


您可以这样做:

按钮


有必要做你想做的事吗?html onclick可以做的任何事情都可以在.on中完成(使用javascript单击/jquery@Alexandros是的,但是onclick代码包含一些通过JSP动态传递的参数。@kpsingh您可以使用数据属性传递参数。
可以从
html onclick
访问的任何变量都可以从
$('button')。在('click',function()上{..
还有,如果是,为什么?我想要两个事件,但都在reverse@kpsingh
if
正在检查按钮是否触发了css类
该类已添加到
else
中,因此第一次按下按钮将运行
else
,然后每再按下一次按钮将运行
if
。谢谢你。它正在运行是的,这是一段棘手的代码。顺便说一句,我不是一个jsp专家,也不能用jquery/javascript更好地修改jsp文件。我的错,我没有从按钮html中删除OnClick
function buttonFunction()
{
  //Do stuff here
}

$('button').on('click', function()
{
  alert('jquery');
  buttonFunction();
});

<button type="button">Button</button>
$(function() {
    var button = $('#button'),
        onclick = button.attr('onclick'); //get onclick value;

    onclick = new Function(onclick); //manually convert it to a function (unsafe)

    button.attr('onclick', null); //clear onclick
    button.click(function() { //bind your own handler
        alert('jquery');
        onclick.call(this); //call original function
    })
});
$('button').on('click', function () {

    var $this = $(this),                //store this so we only need to get it once
        dataVal = $this.data('jspval'); //get the value from the data attribute

    //this bit will fire from the second click and each additional click
    if ($this.hasClass('fired')) {

        alert('jquery'+ dataVal);
    }
    //this will fire on the first click only
    else {

        alert('button');

        $this.addClass('fired'); //this is what will add the class to stop this bit running again
    }

});