对于我的网站,我想向我的网站添加 Facebook 点赞。
这是我使用的代码:
document.write('<iframe id="like-box" src="http://www.facebook.com/plugins/like.php?href='+ like_url +'&layout=button_count&show_faces=true&width=450&action=like&colorscheme=light&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
是包含我的网站网址的字符串。
我希望当用户单击 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。但类似的事情已经被FB抓到了。 这基本上就是我想做的。我尝试过许多在单击 iframe 时触发函数的代码,但没有一个起作用。 例如:
onload="this.contentWindow.document.onclick=function(){alert(\'test\')}"
根本不起作用。
编辑2:
这看起来是一个严重的问题,因为如果我在窗口上使用 onclikc 函数(例如 window.onclick = function(e) {...};
当我点击 iframe 时它不会被触发.有什么想法吗?
最佳答案
代替 document.removeChild(popupDiv)
试试这个:
popupDiv.parentNode.removeChild(popupDiv);
IIRC,您只能使用其父节点来删除节点,而不能使用文档本身。
关于javascript - 点击隐藏 Facebook 点赞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046993/