我正在尝试使用jquery插入电子邮件链接,但如果电子邮件正文中包含&
,则无法使用。
$('#em_button').html('<a href="mailto:?subject=A Photo: ' + js_images[getImg].title + '&body=I thought you would like this photo: ' + thisPage + '?id=' + getGall + '&img=' + js_images[getImg].filename + '"><img src="' + homeUrl + '/' + tUrl + '/img/email.png" title="Email this photo" /></a>');
打破它的部分是&img=
。我尝试过使用&
,但也没有用。有什么建议吗?
注意:如果没有&
,一切正常。
答案 0 :(得分:0)
我认为您的问题是您的查询字符串中有第二个?
。
您应该使用?
替换第二个&
。您的所有&
也应写为&
$('#em_button').html('<a href="mailto:?subject=A Photo: ' + js_images[getImg].title + '&body=I thought you would like this photo: ' + thisPage + '&id=' + getGall + '&img=' + js_images[getImg].filename + '"><img src="' + homeUrl + '/' + tUrl + '/img/email.png" title="Email this photo" /></a>');
答案 1 :(得分:0)
在不知道js_images[getImg]
中的内容的情况下,很难说出现了什么问题。
要记住的一件事是,您应该在创建URL之前始终对组件进行编码。例如:
var x = encodeURIComponent(js_images[getImg].title)
$('#em_button').html('<a href="mailto:?subject=A Photo: '+ x +'&body...')
此外,如果您正在创建类似链接的内容,您可能希望通过编写如下内容来使事情更清晰:
var url = 'mailto:subject=A Photo: ' + encodeURIComponent(js_images[getImg].title) +
'&body=I thought you would like this photo: ' + encodeURIComponent(thisPage) +
'&id=' + encodeURIComponent(getGall) +
'&img=' + encodeURIComponent(js_images[getImg].filename);
var src = encodeURIComponent(homeUrl) + '/' +
encodeURIComponent(tUrl) + '/img/email.png'
var $a = $('<a/>', {
href: url
});
var $img = $('<img/>', {
src: src,
title: 'Email this photo'
});
$a.append($img);
$('#em_button').html($a);
$('#em_button').html('<a href="mailto:?subject=A Photo: ' + js_images[getImg].title + '&body=I thought you would like this photo: ' + thisPage + '?id=' + getGall + '&img=' + js_images[getImg].filename + '"><img src="' + homeUrl + '/' + tUrl + '/img/email.png" title="Email this photo" /></a>');
答案 2 :(得分:0)
你可以尝试这个,希望它有效
'%26img='
答案 3 :(得分:0)
使用+encodeURIComponent('&')+
答案 4 :(得分:0)
如果您只是将URL参数和正文文本附加到一个字符串中,则正文文本中的&
将被解析为URL参数。在PHP中,我会使用urlencode
将这些字符的出现次数编码为%xx
- 值。
看看КонстантинРекунов的答案,我假设您可以使用encodeURIComponent
对身体内容进行编码。
encodeURIComponent( thisPage + '?id=' + getGall + '&img=' + js_images[getImg].filename )
请参阅:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
您可以通过在服务器上使用URL重写图像文件来避免此问题。
而不是:
thisPage + '?id=' + getGall + '&img=' + js_images[getImg].filename
......可能是:
thisPage + '/' + getGall + '/img/' + js_images[getImg].filename
答案 5 :(得分:0)
而不是使用“&amp;”尝试使用&