请看这把小提琴:http://jsfiddle.net/ZWw3Z/5/

我的代码是:

p { position: relative; background-color: blue; } p:before { content: ''; position: absolute; left:100%; width: 10px; height: 100%; background-color: red; } <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate...</p>

我想只在伪元素(红色位)上触发一个单击事件。也就是说,我不希望在蓝色位上触发单击事件。


当前回答

事实上,这是可能的。你可以检查被点击的位置是否在元素之外,因为这只会在::before或::after被点击时发生。

这个例子只检查右边的元素,但这在您的例子中是可行的。

span = document.querySelector('span'); 跨度。addEventListener('点击',函数(e) { if (e.offsetX > span.offsetWidth) { 跨度。className = 'c2'; }其他{ 跨度。className = 'c1'; } }); Div{边距:20px;} span:after{内容:' after ';位置:绝对的;} 跨度。C1{背景:黄色;} 跨度。C2:后面{背景:黄色;} < div > < span >元素< / span > < / div >

JSFiddle

其他回答

不,但你可以这样做

在html文件中添加此部分

<div class="arrow">
</div>

在css中你可以这样做

p div.arrow {
    content: '';
    position: absolute;
    left:100%;
    width: 10px;
    height: 100%;
    background-color: red;
} 

希望对你有所帮助

这对我来说很管用:

$('#element').click(function (e) {
        if (e.offsetX > e.target.offsetLeft) {
            // click on element
        }
         else{
           // click on ::before element
       }
});

在现代浏览器中,您可以尝试使用css属性pointer-events(但这导致无法检测父节点上的鼠标事件):

p {
    position: relative;
    background-color: blue;
    color:#ffffff;
    padding:0px 10px;
    pointer-events:none;
}
p::before {
    content: attr(data-before);
    margin-left:-10px;
    margin-right:10px;
    position: relative;
    background-color: red;
    padding:0px 10px;
    pointer-events:auto;
}

当事件目标是“p”元素时,您就知道它是“p:before”。

如果仍然需要检测主p上的鼠标事件,可以考虑修改HTML结构。您可以添加span标签和以下样式:

p span {
    background:#393;
    padding:0px 10px;
    pointer-events:auto;
}

事件目标现在是“span”和“p:before”元素。

不使用jquery的示例:http://jsfiddle.net/2nsptvcu/

jquery示例:http://jsfiddle.net/0vygmnnb/

下面是支持指针事件的浏览器列表:http://caniuse.com/#feat=pointer-events

这是由Fasoeu用最新的CSS3和JS ES6编辑的答案

编辑的演示没有使用JQuery。

最短的代码示例:

<p><span>Some text</span></p>
p {
    position: relative;
    pointer-events: none;
}
p::before {
    content: "";
    position: absolute;
    pointer-events: auto;
}
p span {
    display: contents;
    pointer-events: auto;
}
const all_p = Array.from(document.querySelectorAll('p'));

for (let p of all_p) {
    p.addEventListener("click", listener, false);
};

解释:

pointer-events control detection of events, removing receiving events from target, but keep receiving from pseudo-elements make possible to click on ::before and ::after and you will always know what you are clicking on pseudo-element, however if you still need to click, you put all content in nested element (span in example), but because we don't want to apply any additional styles, display: contents; become very handy solution and it supported by most browsers. pointer-events: none; as already mentioned in original post also widely supported.

JavaScript部分还使用了广泛支持的Array.from和for…的,但是在代码中不需要使用它们。

这些答案都不可靠,我的答案也不可靠得多。

除了注意事项外,如果您确实遇到了这样一种幸运的情况,即您试图单击的元素没有填充(这样元素的所有“内部”空间都被子元素完全覆盖),那么您可以根据容器本身检查click事件的目标。如果匹配,这意味着您单击了:before或:after元素。

显然,这两种类型(之前和之后)都不可行,但我已经实现了它作为一个hack/速度修复,它工作得很好,没有一堆位置检查,这可能是不准确的,取决于大约一百万个不同的因素。