简体   繁体   English

如何在函数内调用函数

[英]How to call a function within a function

function btn1(){
alert('hello')
}
function btn2(){
alert('anchor') 
}

I want to call the function btn1 and function btn2 separate on clicking the div. 我想在单击div时分别调用函数btn1和btn2。 As it is shown below. 如下图所示。

<div onclick="btn2()" style="position:relative">
<div style="width:50%; float:left; height:50px; background:#ddd">
</div>
<input type="button" value="hello" onclick="btn1()"/>
</div>

If I am getting this right you are probably experiencing bubbling. 如果我做对了,您可能正在冒泡。 You might want to read about bubbling and event propagation as it will probably be clearer than what I can try to explain. 您可能想阅读有关冒泡和事件传播的信息,因为它可能比我尝试解释的更为清楚。 But to solve your issue you want to take a look at jquery's 但是要解决您的问题,您需要看一下jquery的

event.stopPropagation() 

http://www.w3schools.com/jquery/event_stoppropagation.asp http://www.w3schools.com/jquery/event_stoppropagation.asp

The Click Event of the button bubbles his way up to the div. 按钮的Click Event会一直跳到div。 Take a look here how to prevent this: 在这里看看如何防止这种情况:

How to stop event propagation with inline onclick attribute? 如何使用内联onclick属性停止事件传播?

I recommend you use addEventListener in favor of inline event handler. 我建议您使用addEventListener来支持内联事件处理程序。

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

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