需要在我使用javascript添加到页面的按钮的onclick事件上传递一个参数
我有一个javascript,我在我的一些页面上包括它,它将在页面上列出的任何联系人上创建一个按钮。该按钮的目的是让用户能够轻松地通过电子邮件发送联系人,并且该电子邮件将包含诸如网站URL之类的信息 我使用以下include命令包括我的函数调用需要在我使用javascript添加到页面的按钮的onclick事件上传递一个参数,javascript,sharepoint-2013,Javascript,Sharepoint 2013,我有一个javascript,我在我的一些页面上包括它,它将在页面上列出的任何联系人上创建一个按钮。该按钮的目的是让用户能够轻松地通过电子邮件发送联系人,并且该电子邮件将包含诸如网站URL之类的信息 我使用以下include命令包括我的函数调用 <script type="text/javascript" data-Subject="Site" src="../SiteAssets/js-test/AddContactButtons.js"></script> My
<script type="text/javascript" data-Subject="Site" src="../SiteAssets/js-test/AddContactButtons.js"></script>
My AddContactButtons.js的源代码如下:
$(document).ready(function() {
// Get the subject type
var this_js_script = $('script[src*=AddContactButtons]');
var subjectType = this_js_script.attr('data-Subject');
if (typeof subjectType == 'undefined' || subjectType == null || subjectType == ''){
subjectType = "Site";
}
//console.log('subjectType='+subjectType);
addContactButtons(subjectType);
});
function addContactButtons(subjectType){
var listTitle="Contacts";
console.log('addcontactButtons:subjectType='+subjectType);
$("table.ms-listviewtable[summary='"+listTitle+"']>tbody>tr").each(function(){
$(this).append("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub' onclick='javascript:openMail(this);'>");
});
}
function openMail(btn){
var emailString = "mailto:";
var emailID = $(btn).prev("td").text()
//console.log(emailID);
console.log('openMail:subjectType='+subjectType);
emailString += emailID ;
emailString += "?Subject=SharePoint Site Support - Site=";
emailString += _spPageContextInfo.webServerRelativeUrl;;
//alert(emailString);
location.href=emailString;
}
$(文档).ready(函数(){
//获取主题类型
var this_js_script=$('script[src*=AddContactButtons]');
var subjectType=this_js_script.attr('data-Subject');
if(typeof subjectType==“未定义”| | subjectType==null | | subjectType==”){
subjectType=“站点”;
}
//log('subjectType='+subjectType);
添加联系人按钮(subjectType);
});
功能添加联系人按钮(subjectType){
var listTitle=“联系人”;
log('addcontactButtons:subjectType='+subjectType);
$($table.ms listviewtable[summary='+listTitle+']>tbody>tr”)。每个(函数(){
$(此)。追加(“”);
});
}
功能openMail(btn){
var emailString=“mailto:”;
var emailID=$(btn).prev(“td”).text()
//console.log(emailID);
log('openMail:subjectType='+subjectType);
emailString+=emailID;
emailString+=“?主题=SharePoint网站支持-网站=”;
emailString+=\u spPageContextInfo.webServerRelativeUrl;;
//警报(电子邮件字符串);
location.href=emailString;
}
问题是我已经尝试了很多不同的变体,似乎无法将变量subjectType添加到openMail函数中。理想情况下,我希望支持默认的站点支持主题,但我需要选项来发送自定义主题或至少其他一些变体,告诉收到电子邮件的人它是用于支持自定义列表(应用程序)的
任何想法都将不胜感激。谢谢我建议在按钮中添加一个
单击
处理程序,而不是使用onclick
属性,这样您就可以显式地传递所需的参数
即:
const button = $("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub'>");
button.click(() => openMail(button, subjectType));
const按钮=$(“”);
按钮。单击(()=>openMail(按钮,主题类型));
$(文档).ready(函数(){
//获取主题类型
var this_js_script=$('script[src*=AddContactButtons]');
var subjectType=this_js_script.attr('data-Subject');
if(typeof subjectType==“未定义”| | subjectType==null | | subjectType==”){
subjectType=“站点”;
}
//log('subjectType='+subjectType);
添加联系人按钮(subjectType);
});
功能添加联系人按钮(subjectType){
var listTitle=“联系人”;
log('addcontactButtons:subjectType='+subjectType);
$(“.container”).each(函数(){
常量按钮=$(“”);
按钮。单击(()=>openMail(按钮,主题类型));
$(此).append(按钮);
});
}
函数openMail(btn,主题类型){
log('openMail:subjectType='+subjectType);
/*
var emailString=“mailto:”;
var emailID=$(btn).prev(“td”).text()
//console.log(emailID);
log('openMail:subjectType='+subjectType);
emailString+=emailID;
emailString+=“?主题=SharePoint网站支持-网站=”;
emailString+=\u spPageContextInfo.webServerRelativeUrl;;
//警报(电子邮件字符串);
location.href=emailString;
*/
}
选项1:创建一个全局变量“subjectType”来实现它。修改代码如下
var subjectType="";
$(document).ready(function() {
// Get the subject type
var this_js_script = $('script[src*=AddContactButtons]');
subjectType = this_js_script.attr('data-Subject');
if (typeof subjectType == 'undefined' || subjectType == null || subjectType == ''){
subjectType = "Site";
}
//console.log('subjectType='+subjectType);
addContactButtons(subjectType);
});
function addContactButtons(subjectType){
var listTitle="Contacts";
console.log('addcontactButtons:subjectType='+subjectType);
$("table.ms-listviewtable[summary='"+listTitle+"']>tbody>tr").each(function(){
$(this).append("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub' onclick='javascript:openMail(this);'>");
});
}
function openMail(btn){
var emailString = "mailto:";
var emailID = $(btn).prev("td").text()
//console.log(emailID);
console.log('openMail:subjectType='+subjectType);
emailString += emailID ;
emailString += "?Subject=SharePoint Site Support - Site=";
emailString += _spPageContextInfo.webServerRelativeUrl;;
//alert(emailString);
location.href=emailString;
}
var subjectType=”“;
$(文档).ready(函数(){
//获取主题类型
var this_js_script=$('script[src*=AddContactButtons]');
subjectType=this_js_script.attr('data-Subject');
if(typeof subjectType==“未定义”| | subjectType==null | | subjectType==”){
subjectType=“站点”;
}
//log('subjectType='+subjectType);
添加联系人按钮(subjectType);
});
功能添加联系人按钮(subjectType){
var listTitle=“联系人”;
log('addcontactButtons:subjectType='+subjectType);
$($table.ms listviewtable[summary='+listTitle+']>tbody>tr”)。每个(函数(){
$(此)。追加(“”);
});
}
功能openMail(btn){
var emailString=“mailto:”;
var emailID=$(btn).prev(“td”).text()
//console.log(emailID);
log('openMail:subjectType='+subjectType);
emailString+=emailID;
emailString+=“?主题=SharePoint网站支持-网站=”;
emailString+=\u spPageContextInfo.webServerRelativeUrl;;
//警报(电子邮件字符串);
location.href=emailString;
}
选项2:使用jQuery代码实现单击事件
$(document).ready(function() {
// Get the subject type
var this_js_script = $('script[src*=AddContactButtons]');
var subjectType = this_js_script.attr('data-Subject');
if (typeof subjectType == 'undefined' || subjectType == null || subjectType == ''){
subjectType = "Site";
}
//console.log('subjectType='+subjectType);
addContactButtons(subjectType);
});
function addContactButtons(subjectType){
var listTitle="Contacts";
console.log('addcontactButtons:subjectType='+subjectType);
$("table.ms-listviewtable[summary='"+listTitle+"']>tbody>tr").each(function(){
$(this).append("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub'>");
});
$("table.ms-listviewtable[summary='"+listTitle+"']>tbody>tr input[value='Help']").click(function(){
openMail($(this),subjectType);
});
}
function openMail(btn,subjectType){
var emailString = "mailto:";
var emailID = $(btn).prev("td").text()
//console.log(emailID);
console.log('openMail:subjectType='+subjectType);
emailString += emailID ;
emailString += "?Subject=SharePoint Site Support - Site=";
emailString += _spPageContextInfo.webServerRelativeUrl;;
//alert(emailString);
location.href=emailString;
}
$(文档).ready(函数(){
//获取主题类型
var this_js_script=$('script[src*=AddContactButtons]');
var subjectType=this_js_script.attr('data-Subject');
if(typeof subjectType==“未定义”| | subjectType==null | | subjectType==”){
subjectType=“站点”;
}
//log('subjectType='+subjectType);
添加联系人按钮(subjectType);
});
功能添加联系人按钮(subjectType){
var listTitle=“联系人”;
log('addcontactButtons:subjectType='+subjectType);
$($table.ms listviewtable[summary='+listTitle+']>tbody>tr”)。每个(函数(){
$(此)。追加(“”);
});
$(“table.ms listviewtable[summary=”+listTitle+“]>tbody>tr input[value='Help'])。单击(函数(){
openMail($(this),subjectType);
});
}
函数openMail(btn,主题类型){
var emailString=“mailto:”;
var emailID=$(btn).prev(“td”).text()
//console.log(emailID);
log('openMail:subjectType='+subjectType);
emailString+=emailID;
emailString+=“?主题=SharePoint网站支持-网站=”;
emailString+=\u spPageContextInfo.webServerRelativeUrl;;
//警报(电子邮件字符串);
location.href=emailString;
}
console.log('openMail:subjectType='+subjectType)的作用是什么<