pointer-events: none不起作用

19
我正在创建一个网络应用程序,其中我制作了一个可点击的卡片。 我想在零机会的情况下禁用锚标记上的点击事件,但是pointer-events: none;不起作用。 我在这里设置了一个代码片段以便更好地理解。

.fc-card-header {
    background: #1976d2;
    padding: 24px;
    height: auto;
    border-radius: 3px;
    display: block;
}
.svg-icon svg {
    width: 24px;
    height: 24px;
    fill: rgba(0,0,0,0.54);
}
<a href="cmOpportunitySummary" class="white" style="text-decoration:none;pointer-events: none; cursor: default;;">
<div class="fc-card-header">
    <div class="grid-row">
        <div class="grid-cell text-left no-padding padding-right cell-auto-width">
            <div class="svg-icon no-width no-padding white" data-role="ico_RoundStar"><svg viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"></path> <path d="M11.99 2C6.47 2 2 6.48 2 12s4.47 10 9.99 10C17.52 22 22 17.52 22 12S17.52 2 11.99 2zm4.24 16L12 15.45 7.77 18l1.12-4.81-3.73-3.23 4.92-.42L12 5l1.92 4.53 4.92.42-3.73 3.23L16.23 18z"></path></svg></div>
        </div>
        <div class="grid-cell no-padding flex flex-vcenter">
            <p class="text-left white">Opportunities</p>
        </div>
    </div>
    <div class="grid-row padding-top">
        <div class="grid-cell no-padding padding-top padding-bottom text-left flex flex-vcenter">
            <span class="heading white no-line-height">0</span>
        </div>
    </div>
</div>
</a>


在你的函数调用中添加 e.preventDefault(); - Aslam
谢谢@hunzaboy,但我不想使用JavaScript。 - AG_
你有什么理由不能直接删除 href='' 吗? - Turnip
@gavgrif 这不是真的。在 HTML5 中完全有效。https://www.w3.org/TR/2016/REC-html51-20161101/links.html#element-attrdef-a-href - Turnip
@Turnip - 谢谢你教我一件事情 - 我完全以为它是无效的标记。我错了。干杯 :) - gavgrif
2个回答

26

a 标签上使用 display:block; 或者 display:inline-block;,这样会起作用。

a {
  text-decoration: none;
  pointer-events: none;
  cursor: default;
  display: block;
  color: #fff;
}
.fc-card-header {
  background: #1976d2;
  padding: 24px;
  height: auto;
  border-radius: 3px;
  display: block;
}
.svg-icon svg {
  width: 24px;
  height: 24px;
  fill: rgba(0, 0, 0, 0.54);
}
<a href="cmOpportunitySummary" class="white">
  <div class="fc-card-header">
    <div class="grid-row">
      <div class="grid-cell text-left no-padding padding-right cell-auto-width">
        <div class="svg-icon no-width no-padding white" data-role="ico_RoundStar">
          <svg viewBox="0 0 24 24">
            <path d="M0 0h24v24H0z" fill="none"></path>
            <path d="M11.99 2C6.47 2 2 6.48 2 12s4.47 10 9.99 10C17.52 22 22 17.52 22 12S17.52 2 11.99 2zm4.24 16L12 15.45 7.77 18l1.12-4.81-3.73-3.23 4.92-.42L12 5l1.92 4.53 4.92.42-3.73 3.23L16.23 18z"></path>
          </svg>
        </div>
      </div>
      <div class="grid-cell no-padding flex flex-vcenter">
        <p class="text-left white">Opportunities</p>
      </div>
    </div>
    <div class="grid-row padding-top">
      <div class="grid-cell no-padding padding-top padding-bottom text-left flex flex-vcenter">
        <span class="heading white no-line-height">0</span>
      </div>
    </div>
  </div>
</a>


@Sravan pointer-events:none 可以用于所有类型的元素,但在这种情况下,它似乎无法与 a 元素一起使用,可能是因为将块级元素包装成内联元素,或者由于不适当的 URL 格式 <a href="cmOpportunitySummary" class="white"> - Abhishek Pandey
似乎适用于所有行内元素。这是CSS的怪癖吗? - tirmey

0

在我的情况下,我使用了按钮标签。在MAC上的Safari浏览器中添加display: inline-block有所帮助。

<button
    className={props.btnClass} 
    style={{padding:props.btnPadding}}
    onClick={props.btnAction}
>
    {props.btnTitlle}
</button>

以下是 CSS 代码:
button.disabledBtn {
    cursor: not-allowed;
    background: #7eb2ec;
    border: 2px solid #7eb2ec;
    margin-right: 22px;
    font-weight: initial;
    &:active {
      display: inline-block;
      pointer-events: none;
    }
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接