隐藏Facebook喜欢点击

Hide Facebook like on click

本文关键字:喜欢 Facebook 隐藏      更新时间:2023-09-26

对于我的网站,我想添加一个Facebook喜欢我的网站。

下面是我使用的代码:
document.write('<iframe id="like-box" src="http://www.facebook.com/plugins/like.php?href='+ like_url +'&amp;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:60px; height:80px;" allowTransparency="true"></iframe>');
var popupDiv = document.getElementById('like-box');
popupDiv.onclick = function(e){
    if (!e) e = window.event;
    alert("Like button will be removed");
    document.removeChild(popupDiv);
};

其中like_url是包含我的网站url的字符串。

我希望iframe在用户点击它时被删除。但是上面的代码根本不起作用。 onclick函数永远不会触发。有办法使它正常工作吗?

我也试着对twitter按钮做同样的事情,像这样:

document.write('<div id="like-box" style="left:0; top:0; width: 500px; height: 230px;">');
document.write('<scr'+'ipt type="text/javascript" src="https://apis.google.com/js/plusone.js"></scr'+'ipt>');
document.write('<g:plusone href="' + gplus_url + '"></g:plusone>');
document.write('</div>');
var popupDiv = document.getElementById('like-box');
popupDiv.onclick = function(e){
    if (!e) e = window.event;
    alert("G+ button will be removed");
    document.removeChild(popupDiv);
};

但是它也不工作!我试图触发点击窗口,但我得到了相同的结果。

你能帮我解决吗?

编辑:

我会试着用更好的方式来解释它。我想检测点击事件内部或超过一个iframe从主文档。我的意思是:我在我的网站上。我点击facebook喜欢,点击是由我的index.html页面触发的,然后我在几秒钟后删除iframe。但facebook也遇到了类似的情况。这基本上就是我想做的。我已经尝试了许多代码,触发一个函数时,单击iframe,但没有一个工作。例如:
onload="this.contentWindow.document.onclick=function(){alert(''test'')}"

根本不起作用

编辑2:这看起来是一个严重的问题,因为如果我在我的窗口上使用一个onclick函数(例如。当我点击iframe时,window.onclick = function(e) {...};没有被触发。你知道吗?

代替document.removeChild(popupDiv)试试这个:

popupDiv.parentNode.removeChild(popupDiv);

IIRC,您只能使用它们的父节点删除节点,而不能使用文档本身。

最后我找到了这样做的方法(至少对于第一次点击)。如果有人需要代码,在这里:

var inIframe = false;
function checkClick()
{
    if (document.activeElement && document.activeElement === document.getElementById("iframe_id_goes_here"))
    {
        if (inIframe == false)
        {
            console.log("Clicked");
            inIframe = true;
        }
    }
    else inIframe = false;
}
setInterval(checkClick, 200);

感谢那些试图帮助我的人。

删除正文:document.body.removeChild(popupDiv);

试试document.getElementById(popupDiv).innerHTML = "";