Home >Backend Development >PHP Tutorial >javascript - How to avoid a certain element not existing in html, causing js to obtain the attribute information of the element and report an error
The page code is as follows:
<code><empty name="search_data"> <div class="box"> <!-- html代码 --> </div> </empty> <script> var top = $('.box').offset().top; $("html,body").scrollTop(top); </script></code>
As above,
The page code is as follows:
<code><empty name="search_data"> <div class="box"> <!-- html代码 --> </div> </empty> <script> var top = $('.box').offset().top; $("html,body").scrollTop(top); </script></code>
As above,
<code>if($('[name="search_data]"').size()>0){ //当“search_data”数据存在 }else{ //当“search_data”不存在时 }</code>