尝试将标题标记添加到mailto链接,并在正文

时间:2016-05-12 22:33:11

标签: javascript jquery html

我正在为我们的产品创建一个帮助网站。它将由我们的客户服务组在Web上部署(对于已登录的客户),并将与我们的软件一起部署。我们想要创建一个反馈机制,但是我们不能使用服务器端脚本,因为在某些情况下,用户将在本地查看帮助站点。

我们正在制定一个长期解决方案,但目前我们正尝试使用简单的<a href="mailto">链接(我们将其格式化为按钮):

<p class="docfeedback"><a href="mailto:example@example.com?subject=Documentation Feedback" title="We welcome your feedback on this documentation. If you cannot send mail from this server, please send your feedback to: example@example.com>

我想要的是获取<title>元素的值并将其放在Subject中,并且我想在正文行中获取url路径名(仅限路径名)。

我已经阅读了几个答案,这些答案给了我解决方案的一部分,但我希望将它们放在一起,并以正确的方式编码。

我一直在阅读有关使用javascript to get the URL的内容,我能够在邮件正文中成功使用this answer。但后来我尝试使用typeof将主题中的第一个H1作为消息的主题。然后我将其更改为title元素。 (我的项目可以自动访问jquery [1.8.3],所以我不必专门调用它。)

所以,这有效,但我不知道如何将它们放在一起。这是我的HTML中的当前代码。

<script>
  $(document).ready(function(){
  var mailtoHref = $('#mailme a').attr('href'); //get href
  var h1Text = $('title').text(); //get h1 text
  $('#mailme a').attr('href', mailtoHref + h1Text ); //append it to mailto://
  });
</script>
<div id="mailme">
  <p class="docfeedback">
    <a href="mailto:example@example.com?subject=Documentation Feedback for topic: " title="We welcome your feedback on this documentation. If you cannot send mail from this server, please send your feedback to: example@example.com">Send us documentation feedback</a>
  </p>
</div>

&#13;
&#13;
p.docfeedback	/* only used on master page for feedback link */
{
	text-align: center;
	font: bold 11px Arial;
	text-decoration: none !important;
	background-color: #EEEEEE;
	padding: 5px 9px 5px 9px;
	border: 1px solid #CCCCCC;
	border-radius: 10px;
	width: 225px;
}

p.docfeedback a:link,
p.docfeedback a:visited,
p.docfeedback a:hover,
p.docfeedback a:active
{
	color: #666;
	text-decoration: none !important;
}
&#13;
<head>
  <title>This is the name of the document</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  </head>
  <script>/* <![CDATA[ */
				$(document).ready(function(){
				var mailtoHref = $('#mailme a').attr('href'); //get href
				var h1Text = $('title').text(); //get h1 text
				$('#mailme a').attr('href', mailtoHref + h1Text ); //append it to mailto://
				});
			/* ]]> */</script>
            <div id="mailme">
                <p class="docfeedback"><a href="mailto:example@example.com?subject=Documentation Feedback for topic: " title="We welcome your feedback on this documentation. If you cannot send mail from this server, please send your feedback to: example@example.com">Send us documentation feedback</a>
                </p>
            </div>
&#13;
&#13;
&#13;

您可以提供任何帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

用于将当前网址添加到电子邮件正文的jQuery位于

之下
 $(document).ready(function() {
   var url = window.location.href; //get url
   var h1Text = $('title').text(); //get h1 text
   var email = 'example@example.com';
   var body = url;
   var subject = 'Documentation Feedback for topic: '+h1Text;
   var hrefTitle = 'We welcome your feedback on this documentation. If you cannot send mail from this server, please send your feedback to: '+email;
   $('#mailto').attr('href','mailto:'+email+'?body='+body+'&subject='+subject);
   $('#mailto').attr('title', hrefTitle);
 });

html mailto链接现在就像这样

<a id="mailto" href="" title="">Send us documentation feedback</a>

完整代码在这里https://jsfiddle.net/50c99uan/2/

相关问题