我在a中有一个div,但是如何触发div上的事件并阻止<a>?

时间:2016-03-28 10:09:14

标签: events propagation

this is my first question on stack,i am not good at English,sorry for that.

my code can't stop jumping when i click the div. list is a div contains some details. my js:

function clickList(e)
{
    var listAll=e.parentNode.childNodes;
    var list=listAll[1];
    console.log(list);
    if (list.style.display == "none")
    {  
        list.style.display = "block";    
    }
    else
    {    
        list.style.display = "none";    
    } 
    e.stopPropagation();    
}

my html:

<a href="an address">
  <div class="pull-right" onclick="clickList(this)">
    <i class="iconfont">&#xe60a;</i>
  </div>
</a>

1 个答案:

答案 0 :(得分:0)

当您停止事件传播时,您可以在href而不是地址中使用javascript:void(),如下所示:

<a href="javascript:void(0);">
  <div class="pull-right" onclick="clickList(this)">
  <i class="iconfont">&#xe60a;</i>
  </div>
</a>

同时删除e.stopPropagation();在clickList中,因为这将是div对象而不是事件对象。