这个问题已经在这里有一个答案:
- 为什么jQuery的或DOM方法等的getElementById找不到元素? 8个回答
我建立这个小cookiescript上的一个按钮,当点击它创建一个cookie,然后隐藏了消息。
但现在我建立隐藏消息(DIV#饼干),当饼干被SEET功能,但我得到这个错误每次,但我的DIV确实存在:
Uncaught TypeError: Cannot read property 'style' of null
现在,这些都是我使用的脚本,任何人都可以帮忙吗? :)
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>
而这一点,在那里,曾经的“hidediv”给出了错误,但没有工作的一个按钮,当点击:
<script type="text/javascript">
function hidediv() {
if (document.getElementById) {
document.getElementById('cookie').style.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',365)
}
else {
if (document.layers) {
document.hideShow.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',356)
}
else {
document.all.hideShow.style.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',356)
}
}
}
function showdiv() {
if (document.getElementById) {
document.getElementById('cookie').style.visibility = 'visible';
}
else {
if (document.layers) {
document.hideShow.visibility = 'visible';
}
else {
document.all.hideShow.style.visibility = 'visible';
}
}
}
</script>
<script type="text/javascript">
if (document.cookie.indexOf("uscnCookieScriptJS") >= 0) {
alert("yes");
hidediv();
}
else {
alert("no");
}
</script>
该脚本可以在这里找到: http://dev.uscn.nl/cookiescript/v2/