0

我正在尝试使用 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=。我试过使用&amp;,但也没有用。有什么建议么?

注意:如果没有&,一切正常。

4

6 回答 6

0

?我认为您的问题是您的查询字符串中有第二个。

您应该将第二个替换?&amp;. 你&也应该写成&amp;

$('#em_button').html('<a href="mailto:?subject=A Photo: ' + js_images[getImg].title + '&amp;body=I thought you would like this photo: ' + thisPage + '&amp;id=' + getGall + '&amp;img=' + js_images[getImg].filename + '"><img src="' + homeUrl + '/' + tUrl + '/img/email.png" title="Email this photo" /></a>');
于 2012-12-08T22:16:52.630 回答
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>');
于 2012-12-08T22:24:10.443 回答
0

你可以试试这个,希望有用

'%26img='
于 2012-12-08T22:24:10.707 回答
0

采用+encodeURIComponent('&')+

于 2012-12-08T22:24:41.707 回答
0

如果您只是将 URL 参数和正文文本附加到一个字符串中,则&正文文本中的 from 将被解析为 URL 参数。在 PHP 中,我会使用urlencode将此类字符的出现编码为%xx-values。

查看 Константин Рекунов 的答案,我认为您可以使用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
于 2012-12-08T22:25:42.937 回答
0

而不是使用“&”尝试使用&amp;

于 2013-01-12T06:00:17.507 回答