如何防止单击从一个固定的定位元素传递到它后面的元素?

时间:2021-05-28 00:03:38

I have a fixed positioned header which has a searchbox in it and there is some content behind it (The header has higher z-index). I have an event handler which handles the 'focus' event on the searchbox. The problem is that when I click in the searchbox, this event handler triggers, but the click also passes through the header to the elements behind it. I don't want the click to pass through. I tried event.preventDefault(), event.stopPropagating() and event.cancelBubble = true but none of them seems to work.

我有一个固定的定位标头,它有一个搜索框,它后面有一些内容(标题有更高的z索引)。我有一个事件处理程序,它处理searchbox上的“焦点”事件。问题是,当我点击searchbox时,这个事件处理程序会触发,但是点击也会通过标题到后面的元素。我不想让点击通过。我尝试了event. preventdefault()、event. stoppropagating()和事件。2 . cancelBubble = true,但没有一个是有效的。

1 个解决方案

#1


1  

It looks like you're trying to stop the propagation of the focus event.

看起来你试图阻止焦点事件的传播。

Try binding the click event of the searchbox and use event.stopPropagation() on it.

尝试绑定searchbox的单击事件并使用event. stoppropagation()。

#1


1  

It looks like you're trying to stop the propagation of the focus event.

看起来你试图阻止焦点事件的传播。

Try binding the click event of the searchbox and use event.stopPropagation() on it.

尝试绑定searchbox的单击事件并使用event. stoppropagation()。