How to proprly write string using jQuery
I have this bit jQuery with HTML. Unfortunately its not working correctly. Can anyone please advice how it should be please?
var Bred =("<div class='TakeMeTo' style='width:" + width + "px; right: " + width + "'>" + whereTo + "<开发者_开发问答;/div>");
Thank you for your help in advance
I'm not sure what you mean by "it's not working correctly". What exactly is happening? Whatever the case, you're usually better off constructing dynamic HTML this way:
var Bred = $("<div></div>").addClass("TakeMeTo")
.css("width", width + "px").css("right", width)
.text(whereTo);
Use css()
instead of directly setting the style
attribute. Optionally you may wish to use .html(whereTo)
instead, whichever is appropriate.
Why do it this way? Things will be correctly escaped.
I think I found he root of your problem
you need to specify a px unit for the "right" css property, like below:
var Bred =("<div class='TakeMeTo' style='width:" + width + "px; right: " + width + "px;'>" + whereTo + "</div>");
Notice the second [ "px;'>" ] after [ right: " + width + ]
This one works nice. Thanks you all for suggestions
var Bred = $("<div></div>").attr("class", "TakeMeTo").attr("style", "width:" + width + "px; right: " + width + "px").text(whereTo);
精彩评论