3 回答

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
我有一個(gè) javascript 包含在我的一些頁(yè)面上,它將在頁(yè)面上列出的任何聯(lián)系人上創(chuàng)建一個(gè)按鈕。該按鈕的目的是使用戶(hù)可以輕松地向該聯(lián)系人發(fā)送電子郵件,并且該電子郵件將包含諸如站點(diǎn) URL 之類(lèi)的信息。
我使用以下包含我的函數(shù)調(diào)用
<script type="text/javascript" data-Subject="Site" src="../SiteAssets/js-test/AddContactButtons.js"></script>
我的 AddContactButtons.js 有以下來(lái)源:
$(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;
}
問(wèn)題是我嘗試了很多不同的變體,但似乎無(wú)法將變量 subjectType 用于我的 openMail 函數(shù)。理想情況下,我想支持默認(rèn)的站點(diǎn)支持主題,但我需要選擇發(fā)送自定義主題或至少一些其他變體來(lái)告訴收到電子郵件的人它是用于支持自定義列表(應(yīng)用程序) .

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
選項(xiàng) 1:創(chuàng)建一個(gè)全局變量“subjectType”來(lái)實(shí)現(xiàn)它。修改代碼如下。
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;
}
方案二:使用jQuery代碼實(shí)現(xiàn)點(diǎn)擊事件。
$(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;
}

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊
我建議向click按鈕添加一個(gè)處理程序,而不是使用該onclick屬性,以便您可以明確地傳入所需的參數(shù)。
那是:
const button = $("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub'>");
button.click(() => openMail(button, subjectType));
$(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);
$(".container").each(function(){
const button = $("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub'>");
button.click(() => openMail(button, subjectType));
$(this).append(button);
});
}
function openMail(btn, subjectType){
console.log('openMail:subjectType='+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;
*/
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" data-Subject="Site" src="../SiteAssets/js-test/AddContactButtons.js"></script>
<div class="container"></div>
<div class="container"></div>
正如@Bavo 所說(shuō),您當(dāng)前實(shí)施的內(nèi)容存在范圍和上下文問(wèn)題。
添加回答
舉報(bào)