简体   繁体   English

为什么我的onClick给我一个Uncaught ReferenceError:未定义clickState

[英]Why is my onClick giving me an Uncaught ReferenceError: clickState is not defined

Here is my JS code: there is a lot going on, but I just trying to get the click event to work ... I am certain I have more changes to make but I just want to get this basic functionality to work. 这是我的JS代码:发生了很多事情,但是我只是想使click事件起作用。我确定我需要进行更多更改,但是我只是想使此基本功能起作用。

<script type="text/javascript">
        $(document).ready(function () {
            // Map options
            var mapOptions = {
                center: new google.maps.LatLng(37.09024, -100.4179324),
                zoom: 5,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var map = new google.maps.Map(document.getElementById("map"), mapOptions);


            // Markers
            var locations = [];
            var allMarkers = [];

            locations.push({ name: "Chicago", latlng: new google.maps.LatLng(41.878114, -87.629798), state: "Illinois" });
            locations.push({ name: "New York", latlng: new google.maps.LatLng(40.712784, -74.005941), state: "New York" });
            locations.push({ name: "Los Angeles", latlng: new google.maps.LatLng(34.052234, -118.243685), state: "California" });
            locations.push({ name: "Washington DC", latlng: new google.maps.LatLng(38.907192, -77.036871), state: "District of Colombia" });
            locations.push({ name: "Cincinnati", latlng: new google.maps.LatLng(39.103118, -84.512020), state: "Ohio" });
            locations.push({ name: "Houston", latlng: new google.maps.LatLng(29.760427, -95.369803), state: "Texas" });
            locations.push({ name: "Seattle", latlng: new google.maps.LatLng(47.606209, -122.332071), state: "Washington" });
            locations.push({ name: "Miami", latlng: new google.maps.LatLng(25.761680, -80.191790), state: "Florida" });
            locations.push({ name: "Newark", latlng: new google.maps.LatLng(40.735657, -74.172367), state: "New Jersey" });
            locations.push({ name: "Denver", latlng: new google.maps.LatLng(37.739236, -104.990251), state: "Colorado" });

            // Add the Markers to the map
            var bounds = new google.maps.LatLngBounds();

            for (var i = 0; i < locations.length; i++) {

                var marker = new google.maps.Marker({
                    position: locations[i].latlng, map: map, title: locations[i].name,
                    html: "<font color=" + "blue><strong>" + locations[i].state + "</strong></font>"
                });

                bounds.extend(locations[i].latlng);
                var infowindow = new google.maps.InfoWindow();

                google.maps.event.addListener(marker, "click", (function (marker) {
                    return function (e) {
                        infowindow.setContent(this.html);
                        infowindow.open(map, this);
                    }
                })(marker))


            }

            map.fitBounds(bounds);

            function clickState() {
                var latLng = new google.maps.LatLng(41.881832, -87.623177);
                map.panTo(latLng);
            }
        });

    </script>

and here is my markup: this is just a test ... I am playing with creating a way to pan on my map by state: 这是我的标记:这只是一个测试...我正在尝试创建一种按状态在地图上平移的方法:

h1>Community Wildlife Habitat</h1>
<div class="container">
    <div class="row">
        <div class="col-lg-2 col-md-12">
            <h2><u>Links</u></h2>
            <ul style="list-style:none;">
              <!--  <li>@*Html.ActionLink("Forum", "forum", "Home")*@</li> -->
                <li onclick="clickState();">Click on a State</li>
            </ul>
        </div>
        <div class="col-lg-10 col-md-12">
            <div id="map" class="img-responsive" style="width:720px; height: 400px;">
            </div>
        </div>

    </div>
</div> 

You should move your clickState outside the ready function 您应该将clickState移至ready函数之外

<script>
     function clickState() {
            var latLng = new google.maps.LatLng(41.881832, -87.623177);
            map.panTo(latLng);
      }
</script>



<script type="text/javascript">
      $(document).ready(function () {
        .....
</script>

because the inner function ar not visible at window level 因为内部功能ar在窗口级别不可见

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

相关问题 未捕获的ReferenceError:未在onclick上定义 - Uncaught ReferenceError: is not defined onclick 未捕获的ReferenceError:未在onclick上定义obj - Uncaught ReferenceError: obj is not defined onclick 未捕获的 ReferenceError:未使用 onclick 定义函数 - Uncaught ReferenceError: function is not defined with onclick 未捕获的ReferenceError:onclick上未定义函数 - Uncaught ReferenceError: function not defined at onclick 我的 Google 身份验证代码给我错误 ReferenceError: id is not defined - My code for Authentification on Google is giving me the error ReferenceError: id is not defined 未捕获的ReferenceError:我的函数未定义 - Uncaught ReferenceError: my function is not defined Javascript,onclick,未捕获ReferenceError:未定义FAVORITES - Javascript, onclick, Uncaught ReferenceError: FAVOURITES is not defined 未捕获的 ReferenceError: Oncheckboxclicked 未在 HTMLElement.onclick 中定义 - Uncaught ReferenceError: Oncheckboxclicked is not defined at HTMLElement.onclick 未捕获的 ReferenceError:addRecord 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: addRecord is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLInputElement.onclick中未定义functionXXX - Uncaught ReferenceError: functionXXX is not defined at HTMLInputElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM