如何允许svg在链接中可点击

时间:2018-10-23 18:41:44

标签: javascript html css svg

我有以下html:

<a class="delete show-modal" href="#;">
    <svg x="0px" y="0px" width="12px" height="12px" viewBox="0 0 10 10" focusable="false"><polygon class="a-s-fa-Ha-pa" fill="#969696" points="10,1.01 8.99,0 5,3.99 1.01,0 0,1.01 3.99,5 0,8.99 1.01,10 5,6.01 8.99,10 10,8.99 6.01,5 "></polygon></svg>
</a>

当前,我无法单击链接来触发弹出显示。有没有一种方法可以使svg成为“链接文本”的一部分,而无需使用javascript(例如,仅使用css或更改html),还是可以在这里做什么?

此外,这是我触发弹出式窗口的方式:

$(document).on('click', function(e) {

    var clickedElement = $(e.target);
    $this = clickedElement;
    if ($this.hasClass('show-modal')) {

        ...
    }

});

换句话说,我认为不会触发弹出窗口,因为被单击的元素显示为svg而不是a

2 个答案:

答案 0 :(得分:1)

您的问题是svg是单击的目标,并且没有您要查找的类。要解决此问题,您可以将show-modal类添加到锚点内的所有元素,也可以简单地添加以下CSS:

.show-modal *{
  pointer-events: none;
}

pointer-events CSS属性设置在什么情况下(如果有)特定的图形元素可以成为鼠标事件的目标。这是有关pointer-events的更多信息。

查看以下代码段:

$(document).on('click', function(e) {
  var clickedElement = $(e.target);
  $this = clickedElement;
  if ($this.hasClass('show-modal')) {
    alert('it got clicked');
  }
});
.show-modal * {
  pointer-events: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="delete show-modal" href="#;">
  <svg x="0px" y="0px" width="12px" height="12px" viewBox="0 0 10 10" focusable="false"><polygon class="a-s-fa-Ha-pa" fill="#969696" points="10,1.01 8.99,0 5,3.99 1.01,0 0,1.01 3.99,5 0,8.99 1.01,10 5,6.01 8.99,10 10,8.99 6.01,5 "></polygon></svg>
</a>

答案 1 :(得分:0)

您的SVG已包含在您的链接中。也许您的问题是“如何在不使用JavaScript的情况下显示弹出窗口?” 在这种情况下,您可以使用css属性:target

https://codepen.io/imprakash/pen/GgNMXO

<h1>Popup/Modal Windows without JavaScript</h1>
<div class="box">
    <a class="button" href="#popup1">Let me Pop up</a>
</div>

<div id="popup1" class="overlay">
    <div class="popup">
        <h2>Here i am</h2>
        <a class="close" href="#">&times;</a>
        <div class="content">
            Thank to pop me out of that button, but now i'm done so you can close this window.
        </div>
    </div>
</div>

body {
  font-family: Arial, sans-serif;
  background: url(http://www.shukatsu-note.com/wp-content/uploads/2014/12/computer-564136_1280.jpg) no-repeat;
  background-size: cover;
  height: 100vh;
}

h1 {
  text-align: center;
  font-family: Tahoma, Arial, sans-serif;
  color: #06D85F;
  margin: 80px 0;
}

.box {
  width: 40%;
  margin: 0 auto;
  background: rgba(255,255,255,0.2);
  padding: 35px;
  border: 2px solid #fff;
  border-radius: 20px/50px;
  background-clip: padding-box;
  text-align: center;
}

.button {
  font-size: 1em;
  padding: 10px;
  color: #fff;
  border: 2px solid #06D85F;
  border-radius: 20px/50px;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease-out;
}
.button:hover {
  background: #06D85F;
}

.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}

.popup h2 {
  margin-top: 0;
  color: #333;
  font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
.popup .close:hover {
  color: #06D85F;
}
.popup .content {
  max-height: 30%;
  overflow: auto;
}

@media screen and (max-width: 700px){
  .box{
    width: 70%;
  }
  .popup{
    width: 70%;
  }
}