简体   繁体   English

如何从DOM中获取元素并设置onclick事件?

[英]How to get element from DOM and set onclick event?

I have this html element: 我有这个html元素:

<table id="miniToolbar">
     <tbody>          
        <tr><td>
          <button id="btnStrView" type="button" onclick='parent.ExecuteCommand()' class="button_air-medium">
            <img id="streetView" class="miniToolbarContant" src="../stdicons/streetview-icon.png"></button>
          </td></tr> 
    <tbody>
</table>     

as you can see inside button I have on click event: 如您所见,在按钮内部我有点击事件:

onclick='parent.ExecuteCommand()'

And I have this JS function: 我有这个JS函数:

function isMenuItemMasked(item)
{
    var funcId = '75';

     var elem = document.getElementById('btnStrView');

    return false;
}

as you can see inside function I have variable called funcId. 如您所见,在函数内部,我有一个名为funcId的变量。

I need to put this funcId to the on click event: 我需要将此funcId放入on click事件:

onclick='parent.ExecuteCommand('75')'

After I fetch element and put it inside elem variable how do I put funcId as parameter to parent.ExecuteCommand() ? 在获取元素并将其放入elem变量后,如何将funcId作为参数传递给parent.ExecuteCommand()

I think you want to set the function argument dynamically. 我认为您想动态设置函数参数。 Without using external libraries I would do as follows: 不使用外部库,我将执行以下操作:

 function runSubmit() { var value = document.getElementById("text").value; document.getElementById("run").addEventListener('click', function() { func(value); }); } function func(value) { console.log(value); } 
 <input id="text" type="text"> <input type="submit" value="Setup Param" onclick="runSubmit()"> <input id="run" type="submit" value="Run with param"> 

How to use this: When you run the snippet, you will see a text input, a Setup Param button and a Run with param button. 使用方法:运行代码段时,将看到一个文本输入,一个“设置参数”按钮和一个“运行带有参数”按钮。 Insert something in the text input and click Setup Param. 在文本输入中插入一些内容,然后单击“设置参数”。 After, click on Run with param to see the effect 之后,单击“使用参数运行”以查看效果

The input text contains the string that will be used as parameter for func(value) . 输入文本包含将用作func(value)参数的字符串。 The update of #run button callback is triggered by the "Setup param", through the runSubmit() callback. #run按钮回调的更新由“ Setup param”通过runSubmit()回调触发。 This callback adds to the #run element a listener for the 'click' event, that runs a function with the parameter fixed when event occurs. 此回调向#run元素添加了'click'事件的侦听器,该侦听器在事件发生时运行带有固定参数的函数。

This is only a MCVE, you should adapt it to your case scenario. 这只是一个MCVE,您应该根据情况进行调整。

Mh... Actually @jacob-goh gave you this exact solution in a comment while I wrote this... 嗯...实际上@ jacob-goh在我撰写本文时在评论中给了您这个确切的解决方案...

you can use jquery to call you function from inside the function and pass your variable to that function. 您可以使用jquery从函数内部调用函数,并将变量传递给该函数。

function isMenuItemMasked(item)
{
 var funcId = "75";

$(document).ready(function(){
$("#btnStrView").click(function(){
   parent.ExecuteCommand(funcId);
 });
});
}
function ExecuteCommand(your_var){
    alert(your_var);
    //your code
    }    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM