如何使用Jquery以透明颜色禁用隐藏单击功能

时间:2018-06-03 10:02:00

标签: javascript jquery

我正在使用jQuery隐藏点击我的广告,所以我有这个代码:

$(document).ready(function() {
$(".myadcss").click(function() {
$(this).hide(1000) 
    });

现在我想要禁用此功能,如果有人点击广告,那么如果somone点击透明色?

enter image description here

Demo Ad: 728x90

Demo Full Ad:

3 个答案:

答案 0 :(得分:1)

首先,您的代码示例未完成,可能会抛出错误,因为第一个函数未关闭:

$(document).ready(function() {
  $(".myadcss").click(function() {
    $(this).hide(1000)
  });
});

现在,只需将“myadcss”类添加到广告横幅即可。这样,点击功能只会在您的广告横幅上触发,而不会在其他地方触发。

此外,如果您想要防止隐藏横幅,如果点击某个特定元素,您可以检查点击的元素类名称并决定在这种情况下中止您的功能:

$(document).ready(function() {
  $(".myadcss").click(function(event) {
    if(event.target.classList.contains('specific')) {
      return;
    }
    
    $(this).hide(1000)
  });
});
.myadcss {
  height: 200px;
  width: 50px;
  background: red;
}

.specific {
  margin: 30px 5px;
  height: 30px;
  background: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>example</title>
</head>
<body>

  <div class="myadcss">
    Your banner text
    <div class="specific"></div>
  </div>
</body>
</html>

单击白色区域:上面的JavaScript将检查其类名,并将使用return语句中止执行更多代码。

答案 1 :(得分:0)

不是禁用此功能,而是在功能中添加if条件,以便仅在有人点击横幅广告时才会隐藏广告,否则返回广告。

编辑: 从您粘贴的屏幕截图中,如果有人仅点击广告,您似乎想要隐藏广告。如果您仅将myadcss类添加到广告元素中,则点击广告元素外的任何位置都不会触发您的功能。

答案 2 :(得分:0)

来自#this_answer

if (!container.is(e.target) && container.has(e.target).length === 0)

要进行汇总,首先,您在点击横幅广告时使用代码启动hide

$(document).ready(function() {
$(".myadcss").click(function() {
$(this).hide(1000) 
    });

然后监视单击(使用鼠标向上事件)以查看指针是否单击相同的元素或其他元素。如果它是另一个外部元素,则显示您的横幅广告。

$(document).mouseup(function(e) 
{
    var container = $(".myadcss");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
            //Your Logic Here
            // call e.g. banners_show()
        container.show();
    }
});