我無法更改此腳本中的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