document.getElementById不工作,即使我的ID存在于我的CSS [英] document.getElementById not working even if I ve ID present in my css

查看:154
本文介绍了document.getElementById不工作,即使我的ID存在于我的CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试搜索,但没有得到我的问题的有效答案。
当我调用 document.getElementById(#+ id)时,我总是得到null,它警告 code>。

I tried searching but didnt get a valid answer to my question. I always get "null" when I call document.getElementById("#"+id) and it alerts Document not loaded.

我的Javascript代码

My Javascript code

function changeDisplay(id) {
    alert("#"+id);

    if (!document.getElementById("#"+id)) {
        alert("Document Not loaded");
    } else {

        if (id == "property_photos") {
            alert(id);
            document.getElementById("#property_photos").style.display = "block";
            document.getElementById("#property_details").style.display = "none";
            document.getElementById("#property_reviews").style.display = "none";
            alert(document.getElementById(id).style.display);
        }
        if (id == "property_details") {
            alert(id);
            document.getElementById("#property_photos").style.display = "none";
            document.getElementById("#property_details").style.display = "block";
            document.getElementById("#property_reviews").style.display = "none";
            alert(document.getElementById(id).style.display);
        }
        if (id == "property_reviews") {
            alert(id);
            document.getElementById("#property_photos").style.display = "none";
            document.getElementById("#property_details").style.display = "none";
            document.getElementById("#property_reviews").style.display = "block";
            alert(document.getElementById(id).style.display);

        }
    }
}

<li>
    <a href="#property_photos" onclick="changeDisplay('property_photos')">Photos</a>
</li>
<li>
    <a href="#property_details" onclick="changeDisplay('property_details')">Details</a>
</li>
<li>
    <a href="#property_reviews" onclick="changeDisplay('property_reviews')">Reviews</a>
</li>

CSS

#property_photos {
    display:none;
}
#property_details {
    display:block;
}
#property_reviews {
    display:none;
}

任何帮助将不胜感激。它是一个简单的问题。我得到相同的代码,以工作在不同的页面,但不是在页面m工作。我甚至调用.css文件,然后定义该函数,但错误仍然存​​在。

Any help would be grateful. Its a simple question. I got the same code to work on different page but not in page m working on. I have even called .css file before defining the function but the error persists.

推荐答案

document.getElementById() em>需要磅(#)符号。

document.getElementById() does not need the pound (#) symbol.

这篇关于document.getElementById不工作,即使我的ID存在于我的CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆