请看这把小提琴: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>
我想只在伪元素(红色位)上触发一个单击事件。也就是说,我不希望在蓝色位上触发单击事件。
这是由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…的,但是在代码中不需要使用它们。
我将发布解决方案:
Offset()方法获取根元素的左上角信息。
getcomputedstyle()方法获取伪元素的样式。
请确保根元素的位置属性是相对的,而伪元素的位置属性是绝对的。
提琴演示在这里。
希望这能有所帮助。
额外的评论:
这个问题是11年前提出的,现在已经有了一个公认的答案。
我不顾上述事实发布这个解决方案的原因如下。
在伪元素上检测点击事件时,我也有同样的想法。
接受的答案(“添加子元素”)不能满足我的需求。
多亏了这个帖子,我有了解决方案的想法。
如果有人有类似的问题可以通过这篇文章来解决,我会很高兴。
/**
* Click event listener.
*/
$(element).click((e) => {
// mouse cursor position
const mousePoint = { x: e.pageX, y: e.pageY };
if (isMouseOnPseudoElement(mousePoint, e.target, 'before')) {
console.log('[:before] pseudo-element was clicked');
} else if (isMouseOnPseudoElement(mousePoint, e.target, 'after')) {
console.log('[:after] pseudo-element was clicked');
}
});
/**
* Returns the info of whether the mouse cursor is over the pseudo-element.
*
* @param {JSON} point: coordinate of mouse cursor
* @param {DOMElement} element: root element
* @param {String} pseudoType: "before" or "after"
* @returns {JSON}: info of whether the mouse cursor is over the pseudo-element
*/
function isMouseOnPseudoElement(point, element, pseudoType = 'before') {
const pseudoRect = getPseudoElementRect(element, pseudoType);
return point.y >= pseudoRect.top
&& point.y <= pseudoRect.bottom
&& point.x >= pseudoRect.left
&& point.x <= pseudoRect.right;
}
/**
* Gets the rectangle info of the pseudo-element.
*
* @param {DOMElement} element: root element
* @param {String} pseudoType: "before" or "after"
* @returns {JSON}: rectangle info of the pseudo-element
*/
function getPseudoElementRect(element, pseudoType = 'before') {
// top-left info of the root element
const rootOffset = $(element).offset();
// style of the pseudo-element
const pseudoStyle = window.getComputedStyle(element, ':' + pseudoType);
const top = rootOffset.top + parseFloat(pseudoStyle.top);
const left = rootOffset.left + parseFloat(pseudoStyle.left);
const height = parseFloat(pseudoStyle.height);
const width = parseFloat(pseudoStyle.width);
const borderTop = parseFloat(pseudoStyle['border-top-width']);
const borderBottom = parseFloat(pseudoStyle['border-bottom-width']);
const borderLeft = parseFloat(pseudoStyle['border-left-width']);
const borderRight = parseFloat(pseudoStyle['border-right-width']);
// rounds the decimal numbers detected when zooming
return {
top: Math.round(top),
left: Math.round(left),
bottom: Math.round(top + height + borderTop + borderBottom),
right: Math.round(left + width + borderLeft + borderRight)
};
}