点击关闭div

时间:2016-11-18 02:36:41

标签: javascript

我正在使用广告块检测器脚本,并希望使其更加用户友好。使用以下内容,我需要哪些脚本/代码才能让用户单击div来关闭它?

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Detect Adblock - Most effective way to detect ad blockers</title>
    </head>
    <body>

    <style>
    #iQGIuEqKgDvV {
    display: none;
    margin-bottom: 30px;
    padding: 20px 10px;
    background: #D30000;
    text-align: center;
    font-weight: bold;
    color: #fff;
    border-radius: 5px;
    }
    </style>

    <div id="iQGIuEqKgDvV">
      Our website is made possible by displaying online advertisements to our visitors.<br>
      Please consider supporting us by disabling your ad blocker.
    </div>

    <script src="/ads.js" type="text/javascript"></script>
    <script type="text/javascript">

    if(!document.getElementById('Ad-Detect-js')){
      document.getElementById('iQGIuEqKgDvV').style.display='block';
    }

    </script>

    </body>
    </html>

2 个答案:

答案 0 :(得分:0)

这个解决方案怎么样?希望它有所帮助!

&#13;
&#13;
 <div id="handle">
    <div id="slideSource">  Our website is made possible by displaying online advertisements to our visitors.<br>
      Please consider supporting us by disabling your ad blocker.
      </div>
    </div>
&#13;
object = {'2': obj,'3': obj,'10': obj,'11': obj,...}

var array = Object.keys(object);
var newArray = array.map(function(x) {
   return parseInt(x, 10)
});
var newNumber = parseInt(number, 10) // number is from above code, just a number.

console.log(newArray);
console.log(newNumber);
console.log(newNumber in newArray);

console.log(array);
console.log(number.toString());
console.log(number.toString() in array);
    `
&#13;
get('/token')
&#13;
&#13;
&#13;

答案 1 :(得分:0)

向div添加一个事件监听器,如下所示:

document.getElementById("iQGIuEqKgDvV").addEventListener('click',function(){
    //Your code goes here
    //'this' in this function block represents the current element
    this.style.display = 'none';
});

或者这个:

document.getElementById("iQGIuEqKgDvV").onclick = function(){
    //Your code goes here
    //'this' in this function block represents the current element
    this.style.display = 'none';
};

在向元素添加事件侦听器之前,请确保元素已加载

您还可以在onclick中添加div属性来调用函数,如下所示:

<div onclick="hideThis(this)">

然后在你的剧本中:

function hideThis(element){
    element.style.display = 'none';
}