提取&调用元素的onclick HTML属性中定义的JavaScript函数(使用jQuery attr / prop) [英] Extract & call JavaScript function defined in the onclick HTML attribute of an element(using jQuery attr/prop)

查看:85
本文介绍了提取&调用元素的onclick HTML属性中定义的JavaScript函数(使用jQuery attr / prop)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个按钮A& B和我需要调用按钮B的onclick HTML属性中定义的函数,但点击按钮A.



另外我需要将一个额外的参数传递给该函数这个案例。
按钮就像:

 < input type =buttonid =btn_a> 
< input type =buttonid =btn_bonclick =return doSomething(p,q)>

我想打个电话:

  doSomething(p,q,r)//第三个参数用于标识它是通过按钮A 

按钮B实际上是一个可变的按钮&动态获取p,q参数&我需要传递第三个参数。



按钮B可以具有onClick属性,如

onClick =返回doSomething(122,334)或返回doSomething(abc,54)



或其他任何值。
我需要调用这个函数,使得

$ $ p $ doSomething(122,334,'btn_a')或

doSomething(abc,54,'btn_a')

如何使用jQuery / JS ?我可以使用 .prop()来调用函数,但无法通过附加参数传递。



<我可以调用这个函数:

 < script> $('#btn_b')。prop('onclick'))(); // this can invoke('#btn_b')。函数,但如何传递额外的参数?
})
< / script>

提前致谢。 :)

解决方案

你可以这样做,即使用 attr 而不是 prop 来获取b的 onclick 的字符串值,然后替换参数 p,q p,q,r 的字符串中,然后使用函数构造函数创建一个带有新体的匿名函数(替换后)。


$ b $ pre $ jQuery('#btn_a')。on('click',function(){
var onclickB = jQuery ('#btn_b')。attr('onclick'),
onclickA =函数(onclickB.replace(p,q,p,q,r));
return onclickA() ;
});

注意 :您可以这种方法,只需创建一个正则表达式来替换任何你想要的东西。


I am having 2 buttons A & B and I need to call the function defined in onclick HTML attribute of button B but on click of button A.

Also I need to pass an extra param to that function in this case. buttons are like:

<input type="button" id="btn_a">  
<input type="button" id="btn_b" onclick="return doSomething(p,q)">

I want to make a call like:

doSomething(p,q,r) // 3rd param to identify that it was clicked via button A

Button B is actually a variable button & gets the p,q params dynamically & I need to pass the 3rd param along.

button B can have onClick attribute like

onClick= return doSomething(122,334) or return doSomething(abc,54)

or any other values. I need to call the function such that

doSomething(122,334,'btn_a') or

doSomething(abc,54,'btn_a')

how to achieve this using jQuery/JS? I was able to call the function using the .prop() but couldn't figure out passing the additional param.

I was able to call the function like:

<script>
    jQuery('#btn_a').on('click',function(){
      (jQuery('#btn_b').prop('onclick'))(); //this can invoke the function but how to pass the additional param?
    })
  </script>

thanks in advance. :)

解决方案

You can do it like this, i.e. use attr instead of prop to get the string value of b's onclick, afterwards replace the arguments p,q from the string with p,q,r and then use Function constructor to create an anonymous function with the new body (after replace).

jQuery('#btn_a').on('click', function(){
    var onclickB = jQuery('#btn_b').attr('onclick'),
        onclickA = Function(onclickB.replace("p,q", "p,q,r"));
    return onclickA();
});

Note: you can do anything with this approach, just create a regex pattern to replace anything with what you want.

这篇关于提取&amp;调用元素的onclick HTML属性中定义的JavaScript函数(使用jQuery attr / prop)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆