简体   繁体   中英

jQuery “.on( 'click' ,…)” does not work with LET variable declaration. But works VAR

Is this a jquery bug? I have a simple click event. When I use LET to declare some variables with image links, the click event stops working on my ipad (chrome). When I switch to LET all works.

PS: I had another question with the same code but this is completely different.

 // WHEN I USE "VAR" ".on( 'click' ,....)" // works on mobile. With "LET" it does not. let imageLink25 = "http://s020.radikal.ru/i709/1701/58/89fe5a582b92.png "; let imageLink50 = "http: //i056.radikal.ru/1701/3e/0c7dcfd4956b.png"; let imageLink80 = "http://s011.radikal.ru/i318/1701/56/48fc3db701a0.png"; $('.size-list-item').on('click touchend', function(event) { event.preventDefault(); $(".size-list-item").removeClass('active'); $(this).toggleClass('active'); }); 
 .active { font-size: 25px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="menu-list size-list-items"> <a class="size-list-item" data-type="25m2" href="#000" onclick="void(0)">25м2</a> <a class="size-list-item active" data-type="50m2" href="#000" onclick="void(0)">50м2</a> <a class="size-list-item" data-type="80m2" href="#000" onclick="void(0)">80м2</a> </div> 

The let variables is only available in it's block scope. In your case, you can't use the three let variables if they are outside the jQuery block.

Here's a link that explain well the difference between var, let and const.
https://www.sitepoint.com/preparing-ecmascript-6-let-const/

I don't see that you even used the defined variables imageLink* .

It's possible that your browser doesn't support let statements. According to Can I Use? it's fully supported by default in Chrome 49 and above.

If it's not supported it'll cause syntax error preventing JavaScript from further execution. That means, your click event will never be set!

Check which version of Chrome you're running on your iPad. If it's below 49, that's your answer.

However, if the version is between 41 and 48 it's still possible to use let , but you need to be in strict mode . Simply, it requires you to put 'use strict'; at the beginning of your JavaScript code. ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM