简体   繁体   English

如何在列表中添加到onclick函数,即<li onclick=“”><button onclick=“”></button></li>

[英]How to add to onclick function inside a list i.e<li onclick=“”><button onclick=“”></button></li>

Basically, I want to create a sidebar in which there will be many contacts. 基本上,我想创建一个边栏,其中会有很多联系人。 Each contact will be there inside a list. 每个联系人都将在列表内。 There will be two onclick functions inside a list. 列表中将有两个onclick函数。 In list two onclick functions are: 1st onclick- After clicking on the list <li onclick=""></li> It should open another sidebar(I have created it). 在列表中,两个onclick函数是:1st onclick-单击列表后<li onclick=""></li>它应该打开另一个侧边栏(我已经创建了它)。 2nd onclick-This onclick is inside <button> ie <li onclick=""><button onclick=""></button></li> After clicking on this onclick a new chatbox(almost done) should open. 第二次onclick-此onclick在<button><li onclick=""><button onclick=""></button></li>单击此onclick后,应打开一个新的聊天框(几乎完成)。 But in my case whenever I click on any list inside the sidebar, it display's both sidebar and chatbox simultaneously. 但就我而言,每当我单击侧边栏内的任何列表时,它就会同时显示侧边栏和聊天框。 I want both onclick functions to work separately. 我希望两个onclick函数都可以单独工作。

html code: html代码:

<ul class="list mat-ripple">      
  <li ><img src="img1.png"><span class="name"> abc</span> 
     <button class="btn-link" style="margin-left:65px;color:green;font-size:15px;"> 
        <b>   
          <i class="fa fa-comments" aria-hidden="true"> </i>
        </b>
     </button>
  </li>
  <li>
    <img src="img1.png"><span class="name"> abc</span> 
    <button id="clickme" style="margin-left:65px;color:green;font-size:15px;"> 
     <b>   
       <i class="fa fa-comments" aria-hidden="true"> </i>

       </b>
     </button>
  </li>
  <li onclick="toggle_div_fun('sectiontohide');">
    <img src="img1.png"><span class="name"> abc</span> 
    <button  onclick="showFrontLayer();" style="margin-left:65px;color:green;font-size:15px;"> 
       <b>   
        <i class="fa fa-comments" aria-hidden="true"> </i> 
        </b>
    </button>
   </li>      
  </b>
  </button>
  </li>

  <li onclick="toggle_div_fun('sectiontohide');">
     <img src="img1.png"><span class="name"> abc</span> 
     <button onclick="showFrontLayer();" style="margin-left:65px;color:green;font-size:15px;"> 
       <b>   
          <i class="fa fa-comments" aria-hidden="true"> </i>
       </b>
     </button>
  </li>

</ul> 

1st oclick function inside list needs to target below code( 2nd sidebar that needs to toggle using onclick function): 列表内的第一个oclick函数需要定位到代码下方(第二个侧边栏需要使用onclick函数进行切换):

Contact List Sachin Sir 联系人列表Sachin先生

Sachin Sir 萨钦爵士

Sachin Sir 萨钦爵士

Sachin Sir 萨钦爵士

After 2nd Onclick function inside the button is targeting the below code(toggle): 在按钮内的第二次Onclick函数之后,将以下代码作为目标(切换):

 <div id="div1"> <div id="bg_mask"> <div id="frontlayer"><br/><br/><img src="https://png.icons8.com/go-back/androidL/24" title="Go Back" width="24" height="24" onclick="hideFrontLayer();" style="margin-top:-60px;position:absolute;margin-left:-20px;"> <div id='result'></div> <div class='chatcontrols'> <form method="post" onsubmit="return submitchat();"> <input type='text' name='chat' id='chatbox' autocomplete="off" placeholder="ENTER CHAT HERE" /> <input type='submit' name='send' id='send' class='btn btn-send' value='Send' /> <input type='button' name='clear' class='btn btn-clear' id='clear' value='X' title="Clear Chat" /> </form> </div> </div> </div> </div> 

javascript functions: JavaScript函数:

  <script> function showFrontLayer() { document.getElementById('bg_mask').style.visibility='visible'; document.getElementById('frontlayer').style.visibility='visible'; } function hideFrontLayer() { document.getElementById('bg_mask').style.visibility='hidden'; document.getElementById('frontlayer').style.visibility='hidden'; } </script> 

Is there any other code to toggle 2 targeted elements(sidebar and chatbox)? 还有其他代码可以切换2个目标元素(侧边栏和聊天框)吗? Both should be done inside a list 两者都应在列表中完成

  • .

  • Given that you've tagged this with jQuery, I'm going to assume you have jQuery available. 假设您已使用jQuery标记了该标签,我将假设您有可用的jQuery。

    You can attach click events with jQuery, $('button').click(function(){...}) . 您可以使用jQuery $('button').click(function(){...})附加点击事件。

    So what you'd probably want to do is attach a click event that checks the state for certain features and, depending on those features, does different things. 因此,您可能想要做的是附加一个click事件, 事件检查某些功能的状态,并根据这些功能执行不同的操作。

    For example, you could have it check for the class 'clicked', and if it doesn't have the class 'clicked', expand your second sidebar and add the class 'clicked', and if it does have the class 'clicked' you do the second click function you want it to do. 例如,您可以让它检查类“ clicked”,如果没有类“ clicked”,则展开第二个侧边栏并添加类“ clicked”,如果类确实具有“ clicked”您可以执行第二次单击功能。

    Of course depending on the specifics of the desired behavior you may want to check for and change state in a different way than adding the class 'clicked', but that's just one approach. 当然,根据所需行为的具体情况,您可能需要以与添加“单击”类不同的方式来检查和更改状态,但这只是一种方法。

    you can simply use onclick event using jquery like this 您可以像这样简单地使用jquery使用onclick事件

    $(document).ready(function(){
        $('button').click(function(){
            <!---Your Code-->
        });
    });
    

    the problem is about understanding event handling in Javascript. 问题在于了解Java事件处理。 You have a list element with an click event. 您有一个带有click事件的列表元素。 This List Element contains a button, which has a click event too. 该列表元素包含一个按钮,该按钮也具有click事件。 This means, when you click on the list item you trigger 2 events (that's why you see the two divs). 这意味着,当您单击列表项时,将触发2个事件(这就是为什么看到两个div的原因)。 I think you can solve this by using event.stopPropagation() in your showFrontLayer and HideFrontLayer functions, in order to prevent the event target from bubbeling to the list item. 我认为您可以通过在showFrontLayer和HideFrontLayer函数中使用event.stopPropagation()来解决此问题,以防止事件目标冒充列表项。

    function showFrontLayer(evt) {
       evt.stopPropagation();
       document.getElementById('bg_mask').style.visibility='visible';
       document.getElementById('frontlayer').style.visibility='visible';
    }
    function hideFrontLayer(evt) {
      evt.stopPropagation();
      document.getElementById('bg_mask').style.visibility='hidden';
      document.getElementById('frontlayer').style.visibility='hidden';
    }
    

    For more infos about event capturing and bubbeling see: https://javascript.info/bubbling-and-capturing 有关事件捕获和冒泡的更多信息,请参见: https ://javascript.info/bubbling-and-capturing

    I'm guessing what you would need is: 我猜您需要的是:

    event.stopPropagation();
    

    This would prevent the event from button (which is inside) propagating to it's parent. 这样可以防止按钮(位于内部)中的事件传播到其父项。

     $('#inner').click(event=>{ event.stopPropagation(); $('#other').toggle(); }); $('#outer').click(event=>{ console.log("outer"); }); 
     #outer { background-color: blue; width: 100px; height: 100px; padding-top: 50px; } #other { background-color: red; width: 100px; height: 100px; color: white; } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="outer"> <button id="inner">Toggle Visibility</button> </div> <div id="other"> This div has to be toggled on click of the button </div> 

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

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