Home  >  Article  >  Backend Development  >  How to avoid the non-existence of a certain element in html causing js to obtain the attribute information of the element and report an error

How to avoid the non-existence of a certain element in html causing js to obtain the attribute information of the element and report an error

WBOY
WBOYOriginal
2016-09-09 08:13:15888browse

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, is a thinkphp syntax tag, which means that when "search_data" does not exist, the div element with class box will be output to the page. On the contrary, if the "search_data" data exists, the page will not have the div box. , in this case, how to avoid js error?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn