javascript - 尝试将标题标签添加到 mailto 链接,并在正文中添加 URL

标签 javascript jquery html

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

我们正在制定长期解决方案,但目前我们正在尝试通过简单的 <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> 的值元素并将其放入主题中,我想在正文行中获取 url 路径名(仅路径名)。

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

我一直在阅读有关使用 javascript to get the URL 的内容,我能够在消息正文中成功完成此操作。但后来我尝试使用 this answer将主题中的第一个 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>

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;
}
<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>

如果您能提供任何帮助,我将不胜感激。

最佳答案

用于将当前 url 添加到电子邮件正文的 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/

关于javascript - 尝试将标题标签添加到 mailto 链接,并在正文中添加 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37198531/

相关文章:

javascript - event.ctrlKey 在 IE 中不起作用

jquery - Bootstrap 3 : responsive content div in modal box

javascript - jQuery 文件上传,只有单张图片并在上传前重命名

javascript - 无法隐藏字段并根据选择填充字段

JavaScript 无法在 php 标签下运行

javascript - 如何将foreignObject附加到svg图像

javascript - setTimeout 变量中的函数被覆盖

javascript - 如何使用javascript根据日期键对对象数组进行排序?

javascript - 如何在调用按键后显示新问题,同时隐藏以前的问题

html - margin 0 自动;不工作