简体   繁体   中英

Escape “” characters to enter variable in jQuery

I am sure this one is staring me in the face. Trying to increase the rotation on an object until i reaches a value. Getting an error as I am trying to escape the CSS part to enter my variable 'i'.

Can anybody see what may be wrong?

for(var i = 0; i < 200; i++){
  console.log(i);
  $('.rotateMe').css({"transform": "rotate("i"deg)"});
}

Thanks

You have to concatenate variable inside string with + , like this:

for(var i = 0; i < 200; i++){
  console.log(i);
  $('.rotateMe').css({"transform": "rotate("+i+"deg)"});
} 

 for (var i = 0; i < 200; i++) { $('.rotateMe').css({ "transform": "rotate(" + i + "deg)" }); } 
 .rotateMe { width: 50px; height: 50px; background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="rotateMe"></div> 

You forgot the +

 for(var i = 0; i < 200; i++){
    console.log(i);
    $('.rotateMe').css({"transform": "rotate("+i+"deg)"});
 }

您无法在JS中连接字符串,请尝试

"rotate("+i+"deg)"

You have an error in your code:

for(var i = 0; i < 200; i++){
  console.log(i);
  $('.rotateMe').css({"transform": "rotate(" + i + "deg)"});
}

For string concatenation in javascript you must use the plus (+).

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