简体   繁体   English

搜索div ID时Document.getelementbyId返回null

[英]Document.getelementbyId returns null when searching for div id

I am currently trying to show two scroll bars, one at the top and one at the bottom of a ListView. 我目前正在尝试显示两个滚动条,一个在顶部,一个在底部。 However, my document.getelementbyid keeps returning null and I cant figure out why. 但是,我的document.getelementbyid始终返回null,我无法弄清原因。 I am also using a master page if that helps. 如果有帮助,我也使用母版页。

<script type="text/javascript">
    function DoubleScroll(element1) {
        var element = document.getElementById(element1);
        if (!element) return;
        var scrollbar = document.createElement('div');
        scrollbar.appendChild(document.createElement('div'));
        scrollbar.style.overflow = 'auto';
        scrollbar.style.overflowY = 'hidden';
        scrollbar.style.width = '500px';
        scrollbar.firstChild.style.width = element.scrollWidth + 'px';
        scrollbar.firstChild.style.paddingTop = '100px';
        scrollbar.firstChild.appendChild(document.createTextNode('\xA0'));
        scrollbar.onscroll = function () {
            element.scrollLeft = scrollbar.scrollLeft;
        };
        element.onscroll = function () {
            scrollbar.scrollLeft = element.scrollLeft;
        };
        element.parentNode.insertBefore(scrollbar, element);
    }


    DoubleScroll(document.getElementById('doublescroll'));
</script>

Here is the div id I am trying to reach: 这是我要达到的div ID:

<div id="doublescroll" style=" width: 100%; height: auto;">

You are calling document.getElementById twice: once in the function argument and again inside the function. 您两次调用document.getElementById :一次是在函数参数中,另一次是在函数内部。 Remove one of these and it should work. 删除其中之一,它应该可以工作。

PitaJ has one thing right, you are trying to use a DOM reference as an id. PitaJ有一件正确的事,您正在尝试使用DOM引用作为id。 If you want it to be overloaded, you can always do a type check. 如果您希望它超载,则始终可以进行类型检查。

function DoubleScroll(element1) {
    var element = typeof element1 === "string" ? document.getElementById(element1) : element1;

Another problem might be the fact you are calling the element BEFORE it is rendered on the page. 另一个问题可能是您在页面上呈现元素之前调用了该元素。 You can not reference an element before it exists. 您不能在元素存在之前对其进行引用。

Move the line DoubleScroll(document.getElementById('doublescroll')); 移动行DoubleScroll(document.getElementById('doublescroll')); to the bottom of the body, call it onload, or on document ready. 到正文底部,称其为onload或准备好文档。

You are getting NULL because element1 is an object. 因为element1是一个对象,所以您将获得NULL。 When you call document.getElementById inside of the DoubleScroll function, is returns null because getElementById cannot accept an object as a parameter. 当您在DoubleScroll函数中调用document.getElementById时,由于getElementById不能接受对象作为参数,因此返回null。

Here is how you should set element equal to element1: 这是将元素设置为等于element1的方法:

        var element = element1;

You can find a working example here: http://jsfiddle.net/mtruty/Bx2JL/1/ 您可以在这里找到一个有效的示例: http : //jsfiddle.net/mtruty/Bx2JL/1/

All that being said, it is redundant to set element = element1. 综上所述,设置element = element1是多余的。 Why not just use the object you are passing in? 为什么不只使用您要传递的对象?

Hope this help! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM