如何在函数内调用函数

时间:2014-03-13 17:51:10

标签: javascript function

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

我想在单击div时将函数btn1和函数btn2分开调用。如下所示。

<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>

2 个答案:

答案 0 :(得分:3)

如果我做得对,你可能正在经历冒泡。您可能想要阅读有关冒泡和事件传播的内容,因为它可能比我可以尝试解释的内容更清晰。但是要解决你的问题,你想看看jquery的

event.stopPropagation() 

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

答案 1 :(得分:0)

按钮的Click事件一直冒泡到div。看看这里如何防止这种情况:

How to stop event propagation with inline onclick attribute?

我建议你使用addEventListener来支持内联事件处理程序。

相关问题