日期:2014-05-16  浏览次数:20440 次

页面里同样的代码,为什么一个运行,一个不运行?
两个分享按钮代码如下,<div id="L1">是直接写入的,所有功能正常;<div id="L2">是document加载的,审查元素转义正确,和L1的代码相同,却不显示分享按钮:
HTML code

<script type="text/javascript">
var _ga2="全局变量_ga2"
var sj="logo"
</script>

<div id="L1"><script type="text/javascript"charset="utf-8">(function(){var _w=106,_h=24;var param={url:location.href,type:'5',count:'',appkey:'',title:_ga2+'…',pic:'http://csdnimg.cn/www/images/csdnindex_'+sj+'.gif',ralateUid:'',language:'zh_cn',rnd:new Date().valueOf()};var temp=[];for(var p in param){temp.push(p+'='+encodeURIComponent(param[p]||''))};document.write('<iframe allowTransparency="true" frameborder="0" scrolling="no" src="http://hits.sinajs.cn/A1/weiboshare.html?'+temp.join('&')+'" width="'+_w+'" height="'+_h+'"></iframe>')})()</script></div>

<div id="L2"></div>

<script type="text/javascript">
document.getElementById("L2").innerHTML='<sc'+'ript type="text/javascript"charset="utf-8">(function(){var _w=106,_h=24;var param={url:location.href,type:\'5\',count:\'\',appkey:\'\',title:_ga2+\'…\',pic:\'http://csdnimg.cn/www/images/csdnindex_\'+sj+\'.gif\',ralateUid:\'\',language:\'zh_cn\',rnd:new Date().valueOf()};var temp=[];for(var p in param){temp.push(p+\'=\'+encodeURIComponent(param[p]||\'\'))};document.write(\'<iframe allowTransparency="true" frameborder="0" scrolling="no" src="http://hits.sinajs.cn/A1/weiboshare.html?\'+temp.join(\'&\')+\'" width="\'+_w+\'" height="\'+_h+\'"></iframe>\')})()</'+'script>';
</script>



审查元素发现两个div内容相同,但L1后面多了下面的代码:
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://hits.sinajs.cn/A1/weiboshare.html?url=http%3A%2F%2Flocalhost%2Fc%2F2.asp&amp;type=5&amp;count=&amp;appkey=&amp;title=%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F_ga2%E2%80%A6&amp;pic=http%3A%2F%2Fcsdnimg.cn%2Fwww%2Fimages%2Fcsdnindex_logo.gif&amp;ralateUid=&amp;language=zh_cn&amp;rnd=1350755537044" width="106" height="24"></iframe>

请问怎样让L2也和L1一样运行这段代码?

------解决方案--------------------
这个当然。
搜一下:动态加载js。找找
------解决方案--------------------

上一个贴子里不是已经将这个改成一个函数调用方式了么?怎么又来问?
------解决方案--------------------
window.onload=function(){
document.getElementById("L2").innerHTML='<sc'+'ript type="text/javascript"charset="utf-8">(function(){var _w=106,_h=24;var param={url:location.href,type:\'5\',count:\'\',appkey:\'\',title:_ga2+\'…\',pic:\'http://csdnimg.cn/www/images/csdnindex_\'+sj+\'.gif\',ralateUid:\'\',language:\'zh_cn\',rnd:new Date().valueOf()};var temp=[];for(var p in param){temp.push(p+\'=\'+encodeURIComponent(param[p]||\'\'))};document.write(\'<iframe allowTransparency="true" frameborder="0" scrolling="no" src="http://hits.sinajs.cn/A1/weiboshare.html?\'+temp.join(\'&\')+\'" width="\'+_w+\'" height="\'+_h+\'"></iframe>\')})()</'+'script>';
}
还有你的是直接赋值给了控件,这个会执行吗?