简体   繁体   English

防止点击某些元素

[英]Prevent click on certain elements

I'm working on portfolio items, where every item looks like this我正在研究投资组合项目,其中每个项目看起来像这样

<div class="item">
    <div class="item-title"></div>
    <div class="item-subtitle"></div>
    <div class="item-image"></div>
    <div class="item-site"></div>
</div>

Previous div is hidden and items are shown by this code上一个 div 被隐藏,项目由此代码显示

<a class="project" :href="`http://www.${item.site}`"  >

So, if I put like "mysite.com" in item-site div and hover over item output is www.mysite.com and that is ok if item-site is not empty.因此,如果我将“mysite.com”放在 item-site div 中并将鼠标悬停在 item 输出上是 www.mysite.com,并且如果 item-site 不为空,那就可以了。 But if that div is empty output is www.但是如果那个 div 是空的,输出就是 www。 and I dont want that.Is there a way to prevent that?我不想要那个。有办法防止吗? How to dissable click if item-site class is empty, so if I click on it nothing happens, but if is not empty and has link then if I click it's opens that link in new window.如果 item-site 类为空,如何禁用点击,所以如果我点击它没有任何反应,但如果不是空的并且有链接,那么如果我点击它会在新窗口中打开该链接。

You can use the pointer-events: none CSS statement to disable the hover and click events on an element.您可以使用pointer-events: none CSS 语句禁用元素上的悬停和单击事件。

In your case, it might look something like this:在您的情况下,它可能如下所示:

HTML: HTML:

<div class="item">
    <div class="item-title"></div>
    <div class="item-subtitle"></div>
    <div class="item-image"></div>
    <div class="item-site not-clickable"></div>
</div>

CSS: CSS:

.not-clickable{
  pointer-events: none;
}

If the DOM element (in this case <div class="item-site"></div> ) is empty, then add the class not-clickable .如果 DOM 元素(在本例中为<div class="item-site"></div> )为空,则添加类not-clickable If the element has content, then remove the class not-clickable .如果元素有内容,则删除类not-clickable


Please also note that <div> tags are not clickable by default.另请注意,默认情况下<div>标签不可点击。 It sounds like you want these to be links which are <a> tags.听起来您希望这些是<a>标签的链接。 Also, an <a> tag without the href attribute has no pointer events - so an alternative would be to provide the href when you want the element to be clickable, and remove it when you want the element to not be clickable.此外,没有href属性的<a>标记没有指针事件 - 因此,另一种方法是在您希望元素可点击时提供href ,并在您希望元素不可点击时将其删除。

<div class="item">
    <div class="item-title"></div>
    <div class="item-subtitle"></div>
    <div class="item-image"></div>
    <a class="item-site">I should not be clickable</a>
    <a class="item-site" href="https://www.example.com" target="_blank">
      I should be clickable, and I will also open in a new tab
    </a>
</div>

Here is a pen that might explain further: 这是一支可以进一步解释的笔:
https://codepen.io/mikeabeln_nwea/pen/yZQLaj?editors=1111 https://codepen.io/mikeabeln_nwea/pen/yZQLaj?editors=1111

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM