javascript - CSS: Apply pointer-events To Text Only - Stack Overflow

Is there any way to use CSS pointer-events to apply only to the text e.g. in a div? This seems to work

Is there any way to use CSS pointer-events to apply only to the text e.g. in a div? This seems to work with SVGs but is there also a property to apply to regular text elements only?

This is what I am trying to achieve:

<div style="width: 500px; height: 500px">
Fire only when this text is clicked!
<p><!--Don't fire in this space--></p>
Fire here too!
</div>

I do not want to create a child in the parent div to capture the event.

Thanks in advance!

Is there any way to use CSS pointer-events to apply only to the text e.g. in a div? This seems to work with SVGs but is there also a property to apply to regular text elements only?

This is what I am trying to achieve:

<div style="width: 500px; height: 500px">
Fire only when this text is clicked!
<p><!--Don't fire in this space--></p>
Fire here too!
</div>

I do not want to create a child in the parent div to capture the event.

Thanks in advance!

Share Improve this question edited Mar 10, 2017 at 14:47 Ood asked Mar 10, 2017 at 14:37 OodOod 1,8354 gold badges27 silver badges50 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Put it in a <span> and apply your css to it

.mousePointer{
  cursor: pointer;
}
<div style="width: 500px; height: 500px">
  <span style="cursor: pointer;">
    Fire only when this text is clicked!
  </span>
<p><!--Don't fire in this space--></p>
  <span style="cursor: pointer;">
    Fire Here
  </span>
</div>

Or

<div style="width: 500px; height: 500px">
  <span class="mousePointer">
    Fire only when this text is clicked!
  </span>
<p><!--Don't fire in this space--></p>
  <span class="mousePointer">
    Fire Here
  </span>
</div>

Could something like this work?

div {
  pointer-events: auto;
}

div p {
  pointer-events: none;
}

Or select all children like

div * {
  pointer-events: none;
}

I'm not sure on your situation, but a little more html like a span tag would help keep things a little cleaner.

First, there is no text node selector in CSS. So if you don't want to add any children in your parent div, you would have to exclude all other children. For example:

<div>
    No pointer events here
    <div class="other-class1"></div>
    <div class="other-class2"></div>
    <p></p>
    No pointer events here
<div>

Your selector would be something like:

div{
    pointer-events: auto;
}
div:not(.other-class1):not(.other-class2):not(p){
    pointer-events: none;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745616676a4636278.html

相关推荐

  • javascript - CSS: Apply pointer-events To Text Only - Stack Overflow

    Is there any way to use CSS pointer-events to apply only to the text e.g. in a div? This seems to work

    6小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信