我无法更改此脚本中的href。但是innerHTML起作用。哪里不对?通过javascript更改href
<script>
FB.init({
appId: 1234,
cookie: true,
status: true,
xfbml: true
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
document.getElementById('test').href = 'http://www.example.com/auth/login';
// document.getElementById('index-facebook-login-button').innerHTML = '<a class="index-facebook-login-button" href="http://www.example.com/auth/login" target="_self"></a>';
} else if (response.status === 'not_authorized') {
document.getElementById('test').href += 'http://www.example.com/connectfacebook';
//this works!!!
//document.getElementById('index-facebook-login-button').innerHTML = '<a class="index-facebook-login-button" href="http://www.example.com/connectfacebook" target="_self"></a>';
} else {
document.getElementById('test').href += 'http://www.example.com/connectfacebook';
//this works!!!
// document.getElementById('index-facebook-login-button').innerHTML = '<a class="index-facebook-login-button" href="http://www.example.com/connectfacebook" target="_self"></a>';
}
});
</script>
<div id="index-facebook-login-text" onLoad="FB.getLoginStatus();">
some text here
<div id="index-facebook-login-button" style="margin-left: 25px;">
<a id="test" class="index-facebook-login-button" href="" target="_self"></a>
</div>
</div>
也许此刻的你正在尝试访问元素,它不存在或没有该ID?设置一个断点,检查DOM结构......通常的调试过程。 – 2013-04-08 13:59:32
为什么'href + ='在第二和第三种情况下?没有什么意义(除非当前的URL已经有一个空的参数了) – CBroe 2013-04-08 13:59:33