小编Dar*_*son的帖子

为什么我的元素 ID 返回 Null?

代码片段:HTML

               <div id="imgpreview" name="imgpreview">
                   <img id="srcimg" name="srcimg" />

                   <div id="resident_name"></div>
                   <div id="resident_addr"></div>
                   <div id="resident_contact"></div>
                   <div id="sale_or_rent">
                       <img id="for_sale_img" class="obtn" src="../images/ads/ForSaleSignSm.jpg" />
                       <img id="for_rent_img" class="obtn" src="../images/ads/ForRentSignSm.gif" />
                   </div>
               </div>
Run Code Online (Sandbox Code Playgroud)

代码片段:Javascript

function div_obj( id )
{
    if( document.layers ) return document.layers[id];

    else if( document.all ) return document.all[id];

    else if( document.getElementById ) return document.getElementById( id );

    else return null;
}
Run Code Online (Sandbox Code Playgroud)

返回“imgpreview”DIV 的 ID,但所有其他 IMG 和 DIV 返回 null。无论我是否为“srcimg”IMG 定义了 SRC,它都会发生。任何人都知道为什么会发生这种情况?我已经盯着这个很久了,我相信一双新的眼睛会有所帮助。TIA。

html javascript null

2
推荐指数
1
解决办法
1603
查看次数

标签 统计

html ×1

javascript ×1

null ×1