从变量中删除斜杠 [英] Slashes removed from variable
问题描述
我有一个从图像的源属性获得的变量。当我使用style标签将该变量设置为元素的背景时,所有斜杠都被剥离出来。以下是我的一些代码:
var logo = $('。logo-list li.selected-logo img')。 ATTR( 'SRC');
$('。stage')。append('< div class =doc-header>< div class =product-logo>< / div>< div class = logo-smallstyle =background:url('+ logo +');>< / div>< / div>');
当我为变量'logo'设置警告时,它看起来很好:
http://www.example.com/images/img.jpg
但是,当jQuery附加代码时,背景属性的URL如下所示:http:example.com images img.jpg
原因是斜杠被删除/逃脱,我只是不知道为什么。任何人都可以解释这一点吗?
感谢@ BradM的建议,我可以通过使用decodeURIComponent来修复它。
var logo = $('。logo-list li.selected-logo img')。attr('src');
var logo = decodeURIComponent(logo);
I have a variable that I am getting from an image's source attribute. When I set that variable as the background of an element using the style tag, all of the slashes are stripped out of it. Here is some of my code:
var logo = $('.logo-list li.selected-logo img').attr('src');
$('.stage').append('<div class="doc-header"><div class="product-logo"></div><div class="logo-small" style="background:url('+logo+');"></div></div>');
When I set an alert for the variable 'logo' it looks fine: http://www.example.com/images/img.jpg
But when jQuery appends the code, the background attribute's URL looks like this: http: example.com images img.jpg
For some reason the slashes are getting removed/escaped, I just don't know why. Can anyone shed light on this?
I was able to fix it by using decodeURIComponent, thanks to @BradM's suggestion.
var logo = $('.logo-list li.selected-logo img').attr('src');
var logo = decodeURIComponent(logo);
这篇关于从变量中删除斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!