我设置了一个脚本,以便我可以在网站上嵌入各种Facebook帖子。这个想法是,它将随机加载一个帖子,然后在按钮点击加载另一个。当我点击按钮时,之前的帖子消失了,但是我要求的新帖子没有出现。
任何人都可以放弃任何光明吗?
使用Javascript:
var comps = new Array();
comps[0] = "\"https://www.facebook.com/permalink.php?story_fbid=10151746360309352&id=9212109351\"";
comps[1] = "\"https://www.facebook.com/photo.php?fbid=10202149995929199&set=a.3217789641447.156870.1167545588&type=1\"";
var firstLine = "<div id=\"fb-root\"></div>";
var secondLine = "<div class=\"fb-post\" data-href=";
var thirdLine = " data-width=\"550\"></div>";
var randomComp = firstLine + secondLine + comps[Math.floor(Math.random()* comps.length)] + thirdLine;
window.onload=function(){
document.getElementById('compsContainer').innerHTML = randomComp;
}
function changeComp(){
document.getElementById('compsContainer').innerHTML = randomComp;
}
HTML:
<div id="compsContainer">
</div>
<input type='button' onclick='changeComp()' value='Change Comp'/>
答案 0 :(得分:0)
将onlick =''更改为onclick ='javascript:changeComp()'?
好的,我误解了你的问题。
试试这个:
function newFunction(){
var comps = new Array();
comps[0] = "\"https://www.facebook.com/permalink.php?story_fbid=10151746360309352&id=9212109351\"";
comps[1] = "\"https://www.facebook.com/photo.php?fbid=10202149995929199&set=a.3217789641447.156870.1167545588&type=1\"";
var firstLine = "<div id=\"fb-root\"></div>";
var secondLine = "<div class=\"fb-post\" data-href=";
var thirdLine = " data-width=\"550\"></div>";
var randomComp = firstLine + secondLine + comps[Math.floor(Math.random()* comps.length)] + thirdLine;
return randomComp;
}
window.onload=function(){
document.getElementById('compsContainer').innerHTML = newFunction();
}
function changeComp(){
document.getElementById('compsContainer').innerHTML = newFunction();
}
当你调用changeComp时,它不会运行:
var randomComp = firstLine + secondLine + comps[Math.floor(Math.random()* comps.length)] + thirdLine;
所以randomComp将为空。
(更新)
答案 1 :(得分:0)
如果html页面中没有任何其他按钮。试试这个,它会起作用。
<input type='submit' onsubmit='changeComp()' value='Change Comp' />