在javascript div中的文本,然后单击任何地方以关闭div

时间:2014-03-29 11:34:46

标签: javascript

我有一个javascript,它会在冲浪者进入我的网站之前打开一个窗口,该网站有24小时的cookie意味着在24小时后冲浪者再次访问我的网站后会再次弹出。 现在,如果按下红色的“x”按钮,则只能关闭div。另外,当我运行它时,我只能添加图像或iframe,但我想添加一些文字,例如“欢迎来到我的网站”。

这是代码

var floatingAd={
    run: function (e, t) {
        if (e === 2 || e === 1 && (this.getCookie("floatingAd") === null || this.getCookie("floatingAd") === "")) {
            document.write('<div id="floatingAdFixed" style="bottom: 0px; background: #fff; font-family: Arial, Helvetica, sans-serif; left: 0; padding: 0px 0; position: fixed; font-size: 16px; width: 100%; z-index: 99999; float: left; vertical-align: middle; margin: 0px 0 0; opacity: 0.88; font-weight: bold;">');
            document.write('<div style="text-align: right; width: 710px; margin: 340px auto;"><img onclick="floatingAd.close(' + e + ');" style="position: absolute; margin-top: -11px; margin-left: -23px; cursor: pointer;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAYAAAAGAB4TKWmAAAABmJLR0QA/wD/AP+gvaeTAAAF4klEQVRIx7VVbUxTVxg+5972ln5CK/KNAhMFhQ0pBbqg0fFHnPthZGKyxMSYzE1xJo4ax4xz0/lDlhjRqPHHSNQsMTNmP9wi8QOcRgEBFUFEGLRIQT6kpdBC23vO2Xvu1DgVsz9r+vbe3p7zPs/7vM97itD//MKvP2gvL5870tFxEDHGTMnJP+SdOTPwrgR3d+xImujq2stzxa9YUblo9+7RWRc/3LrVXF9cfHXg1Ck6WFPDrixb1tS6cWPCrMm3b0+4VlzcNHbtGh2prWXX16y52nPkiPmtFTzYtMk85nReTF+71m7QapEAMYMxajt5st2Smroq9/TpwVc3tm7ZkuDt7r60dO/eLF1cHEKiiHwuF3p4+PCthJUrP0mvqPC8BGgpK4ueGBo6997q1R8ZBAEJksREvR6LBgOaBqnunTjR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Cqoz4ciJicFc3q6Y9LpfAhrzwocgBCSJkmSGB4dZQSOAAgkQ3Nl7nXYCMnZXy0tdwwREasgSuC+WU5LQzK4ja+ReTWwT4YIjY0hjVotyrKcxnMrAHqt9vjw48d1gtWqSMEBQALM7ckWLUJ9bW0tMqWf2uvr3TxClJbyZygjAw5fqqyH5MpeMS8PDXd31+k0muP/avIfOTlm8PrFxJSUD+WmJjgYMZYKC9GAy/XA6fOVfN7Z6X7VccczMxMXmEyXkubNywrevs1PPCwWFKBBl+sWCYfXfNzW5nnDpr9mZ5t1KtX55PT0lXBMIrfT2QzJ137R1eV+2xAdW7iQg/yWmJpq5ZYZ6Omp84dCpes7OjyzDtpncXHp7xsMB/lANXq9P44T8iQATvJB0yegHwSADWCCSLAu//eLVqmSC6OivoVE+P7UVOUvT592v3OSCywWy2Q4LMJBgJSA5AFI7gdHzXDLwhqYa6SHwdMDiAQgInyHUUQmtZo0eTzjr+b7G6/KDfXpZXvRAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDEwLTAyLTEwVDEyOjQyOjM1LTA2OjAw75HxegAAACV0RVh0ZGF0ZTptb2RpZnkAMjAwOS0wNi0yNFQxOTo0MzozMi0wNTowMBoOvs4AAAAASUVORK5CYII="></div>');
            document.write('<div id="floatingAd" style="text-align: center; width: 710px; margin: 300px auto; ">');
            document.write("</div>");
            document.write("</div>");
            document.getElementById("floatingAd").innerHTML = t
        }
    },
    close: function (e) {
        if (e === 1) {
            this.setCookie("floatingAd", 1, 1)
        }
        document.getElementById("floatingAdFixed").style.display = "none"
    },
    setCookie: function (t, n, r) {
        var i = new Date;
        i.setDate(i.getDate() + r);
        var s = escape(n) + (r == null ? "" : "; expires=" + i.toUTCString());
        document.cookie = t + "=" + s + ";path=/"
    },
    getCookie: function (e) {
        var t = document.cookie;
        var n = t.indexOf(" " + e + "=");
        if (n == -1) {
            n = t.indexOf(e + "=")
        }
        if (n == -1) {
            t = null
        } else {
            n = t.indexOf("=", n) + 1;
            var r = t.indexOf(";", n);
            if (r == -1) {
                r = t.length
            }
            t = unescape(t.substring(n, r))
        }
        return t
    }
}
/**
 * Run the script
 * 1 = 24 hours cookie, 2 = refresh
 */

floatingAd.run(1, '');

两个问题:

  1. 如果点击屏幕上的任意位置,如何关闭它?
  2. 我是否要在floatingAd.run(1, '');
  3. 中显示文字

1 个答案:

答案 0 :(得分:0)

1次退房How do I detect a click outside an element?

编辑:如果您希望关闭窗口,如果&#34;您点击屏幕上的任意位置&#34;,只需按以下方式使用您的对象:

$('html').click(function() {
    // You may skip the following if statement, it may work without it
    if (document.getElementById("floatingAdFixed").style.display != "none")
        // The following line is what you'll definitely need
        floatingAd.close(1);
});

对于2,只需将其添加为run中的另一个变量,就像这样

var floatingAd={
    run: function (e, t, text) {
    // ...
    document.write('<div id="textContainer">' + text + '</div>');
    // ...
}

然后像这样使用它:

floatingAd.run(1, '', 'Writing some text');

<强> DEMO HERE