How do I put a variable in a string using javascript

前端 未结 4 580
慢半拍i
慢半拍i 2021-01-26 05:51

How do I put a variable in a string in JavaScript?

Example:

$(\'#contactForm\').animate({\"marginLeft\": \"+=680px\"}, \"slow\");

I wou

相关标签:
4条回答
  • 2021-01-26 05:54
    var size = "680px";
    $('#contactForm').animate({"marginLeft": "+="+size}, "slow");
    
    0 讨论(0)
  • 2021-01-26 05:55

    You can use the + operator to concatenate your value in a variable to the "+=" string:

    $('#contactForm').animate({"marginLeft": "+=" + size}, "slow");
    
    0 讨论(0)
  • 2021-01-26 06:00
    var theWidth = "680px"
    $('#contactForm').animate({marginLeft: "+=" + theWidth}, "slow");
    
    0 讨论(0)
  • 2021-01-26 06:04

    As with most languages that don't have sigals, you can't perform interpolation, you have to concatenate multiple strings.

    "foo" + "bar" + "baz"
    "foo" + string_var + "baz"
    "+=" + string_var + "px"
    
    0 讨论(0)
提交回复
热议问题