奈何兩字拉成橋、不得不雙擊兩次才能啟用的JS方法

來源:互聯網
上載者:User

標籤:hone   angular   open   site   flash   點擊   inpu   tor   刪掉   

Aug 18, 2017, 豔陽高照

REQUEST

vv在做一個內建Google地圖功能的Microsite,經緯度都研究明明白白的了,卻被一個小問題困擾著,每次重新整理頁面之後點選連結都必須點擊兩次才能啟用功能,而之後只需要點一次就可以,這個小哥用最近新學的AngularJs寫的功能,抓耳撓腮不知道是AngularJs哪裡出了問題

SOLUTION

 在Google裡邊搜了一下“javascript have to click twice”,竟然有好多人遇到相同的問題,開啟了幾個stackoverflow,看了其中兩個,發現說的都是同一個情況,《I have to click twice to activate function using jQuery》(https://stackoverflow.com/questions/28746323/i-have-to-click-twice-to-activate-function-using-jquery)在htm裡邊用onclick引用了JS裡寫的方法,問題就出現在JS裡對應的function中重複綁定了click功能。看到這個答案,反觀自己的HTML和JS代碼

<h4>Where you‘ll find us</h4><div class="clearfix"> <span class="search-store-icon">Region:</span>  <input type="text" placeholder="" name="" class="search-store" ng-model="searchStores" ng-change="timeflash()">  <ul class="storelist" ng-show="searchStores.length > 0" ng-focus="hasFocus=true">    <li ng-repeat="x in stores | filter:searchStores | orderBy:‘region‘" ng-mouseover="mouseOverEvent($index)" ng-click="enterKeyEvent($event)" data-storeidx="{{ x.storeidx }}" data-omni-type="microsite" data-omni="{{ x.omn }}">{{ x.name }}</li>  </ul></div>
Html Code
$scope.enterKeyEvent = function (event) {    var listArry = $(‘.storelist>li‘);    listArry.click(function() {        var selectListName = $(event.target).text();        var selectListIdx = $(event.target).attr(‘data-storeidx‘);        var lat1 = $scope.stores[selectListIdx].lat;        var lng1 = $scope.stores[selectListIdx].lng;        var selectStoreMap = $scope.stores[selectListIdx].map;        $(‘.storelist‘).hide();        $(‘.search-store‘).val(selectListName);        storeName.text($scope.stores[selectListIdx].name);        storeGegion.text($scope.stores[selectListIdx].region);        storeLocation.text($scope.stores[selectListIdx].location);        storeAddr.text($scope.stores[selectListIdx].address);        storePhone.text($scope.stores[selectListIdx].tel);        if(selectStoreMap == null || selectStoreMap == undefined || selectStoreMap == ‘‘){            $(‘#store-map‘).hide();        }else{            $(‘#store-map‘).show();            storeMap.attr("src",selectStoreMap);        }        getNearbyStore(lat1,lng1);    });};
JavaScript Incorrect Code

在Html中“ng-click="enterKeyEvent($event)"”的ng-click就相當於onclick,在JS中定位了listArry,並對listArry綁定了cilck方法,和stackoverflow中提到的問題是同一類,於是刪掉不必要的listArry定位和click方法,重新修改JS如下:

$scope.enterKeyEvent = function (event) {    var selectListName = $(event.target).text();    var selectListIdx = $(event.target).attr(‘data-storeidx‘);    var lat1 = $scope.stores[selectListIdx].lat;    var lng1 = $scope.stores[selectListIdx].lng;    var selectStoreMap = $scope.stores[selectListIdx].map;    $(‘.storelist‘).hide();    $(‘.search-store‘).val(selectListName);    storeName.text($scope.stores[selectListIdx].name);    storeGegion.text($scope.stores[selectListIdx].region);    storeLocation.text($scope.stores[selectListIdx].location);    storeAddr.text($scope.stores[selectListIdx].address);    storePhone.text($scope.stores[selectListIdx].tel);    if(selectStoreMap == null || selectStoreMap == undefined || selectStoreMap == ‘‘){        $(‘#store-map‘).hide();    }else{        $(‘#store-map‘).show();        storeMap.attr("src",selectStoreMap);    }    getNearbyStore(lat1,lng1);};
JavaScript Correct Code

困擾vv昨晚一晚的問題,我們半個小時就解決了,哈,因為今天中午他請客吃烤魚了!

奈何兩字拉成橋、不得不雙擊兩次才能啟用的JS方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.