猿问

需要在我使用 javascript 添加到我的页面的按钮的 onclick 事件上传递参数

我有一个 javascript 包含在我的一些页面上,它将在页面上列出的任何联系人上创建一个按钮。该按钮的目的是使用户可以轻松地向该联系人发送电子邮件,并且该电子邮件将包含诸如站点 URL 之类的信息。


我使用以下包含我的函数调用


<script type="text/javascript" data-Subject="Site" src="../SiteAssets/js-test/AddContactButtons.js"></script>

我的 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;

}

问题是我尝试了很多不同的变体,但似乎无法将变量 subjectType 用于我的 openMail 函数。理想情况下,我想支持默认的站点支持主题,但我需要选择发送自定义主题或至少一些其他变体来告诉收到电子邮件的人它是用于支持自定义列表(应用程序) .


回首忆惘然
浏览 158回答 3
3回答

holdtom

我有一个 javascript 包含在我的一些页面上,它将在页面上列出的任何联系人上创建一个按钮。该按钮的目的是使用户可以轻松地向该联系人发送电子邮件,并且该电子邮件将包含诸如站点 URL 之类的信息。我使用以下包含我的函数调用<script type="text/javascript" data-Subject="Site" src="../SiteAssets/js-test/AddContactButtons.js"></script>我的 AddContactButtons.js 有以下来源:$(document).ready(function() {&nbsp;&nbsp;&nbsp; &nbsp; // Get the subject type&nbsp; &nbsp; var this_js_script = $('script[src*=AddContactButtons]');&nbsp; &nbsp; var subjectType = this_js_script.attr('data-Subject');&nbsp;&nbsp; &nbsp; if (typeof subjectType == 'undefined' || subjectType == null || subjectType == ''){&nbsp; &nbsp; &nbsp; subjectType = "Site";&nbsp; &nbsp; }&nbsp; &nbsp; //console.log('subjectType='+subjectType);&nbsp; &nbsp; addContactButtons(subjectType);});function addContactButtons(subjectType){&nbsp; &nbsp; var listTitle="Contacts";&nbsp; &nbsp; console.log('addcontactButtons:subjectType='+subjectType);&nbsp; &nbsp; $("table.ms-listviewtable[summary='"+listTitle+"']>tbody>tr").each(function(){&nbsp; &nbsp; &nbsp; &nbsp; $(this).append("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub' onclick='javascript:openMail(this);'>");&nbsp; &nbsp; });}function openMail(btn){&nbsp; &nbsp; var emailString = "mailto:";&nbsp; &nbsp; var emailID = $(btn).prev("td").text()&nbsp; &nbsp; //console.log(emailID);&nbsp; &nbsp; console.log('openMail:subjectType='+subjectType);&nbsp; &nbsp; emailString += emailID ;&nbsp; &nbsp; emailString += "?Subject=SharePoint Site Support - Site=";&nbsp; &nbsp; emailString += _spPageContextInfo.webServerRelativeUrl;;&nbsp; &nbsp; //alert(emailString);&nbsp; &nbsp; location.href=emailString;}问题是我尝试了很多不同的变体,但似乎无法将变量 subjectType 用于我的 openMail 函数。理想情况下,我想支持默认的站点支持主题,但我需要选择发送自定义主题或至少一些其他变体来告诉收到电子邮件的人它是用于支持自定义列表(应用程序) .

婷婷同学_

选项 1:创建一个全局变量“subjectType”来实现它。修改代码如下。var subjectType="";$(document).ready(function() {&nbsp;&nbsp;&nbsp; &nbsp; // Get the subject type&nbsp; &nbsp; var this_js_script = $('script[src*=AddContactButtons]');&nbsp; &nbsp; subjectType = this_js_script.attr('data-Subject');&nbsp;&nbsp; &nbsp; if (typeof subjectType == 'undefined' || subjectType == null || subjectType == ''){&nbsp; &nbsp; &nbsp; subjectType = "Site";&nbsp; &nbsp; }&nbsp; &nbsp; //console.log('subjectType='+subjectType);&nbsp; &nbsp; addContactButtons(subjectType);});function addContactButtons(subjectType){&nbsp; &nbsp; var listTitle="Contacts";&nbsp; &nbsp; console.log('addcontactButtons:subjectType='+subjectType);&nbsp; &nbsp; $("table.ms-listviewtable[summary='"+listTitle+"']>tbody>tr").each(function(){&nbsp; &nbsp; &nbsp; &nbsp; $(this).append("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub' onclick='javascript:openMail(this);'>");&nbsp; &nbsp; });}function openMail(btn){&nbsp; &nbsp; var emailString = "mailto:";&nbsp; &nbsp; var emailID = $(btn).prev("td").text()&nbsp; &nbsp; //console.log(emailID);&nbsp; &nbsp; console.log('openMail:subjectType='+subjectType);&nbsp; &nbsp; emailString += emailID ;&nbsp; &nbsp; emailString += "?Subject=SharePoint Site Support - Site=";&nbsp; &nbsp; emailString += _spPageContextInfo.webServerRelativeUrl;;&nbsp; &nbsp; //alert(emailString);&nbsp; &nbsp; location.href=emailString;}方案二:使用jQuery代码实现点击事件。$(document).ready(function() {&nbsp;&nbsp;&nbsp; &nbsp; // Get the subject type&nbsp; &nbsp; var this_js_script = $('script[src*=AddContactButtons]');&nbsp; &nbsp; var subjectType = this_js_script.attr('data-Subject');&nbsp;&nbsp; &nbsp; if (typeof subjectType == 'undefined' || subjectType == null || subjectType == ''){&nbsp; &nbsp; &nbsp; subjectType = "Site";&nbsp; &nbsp; }&nbsp; &nbsp; //console.log('subjectType='+subjectType);&nbsp; &nbsp; addContactButtons(subjectType);});function addContactButtons(subjectType){&nbsp; &nbsp; var listTitle="Contacts";&nbsp; &nbsp; console.log('addcontactButtons:subjectType='+subjectType);&nbsp; &nbsp; $("table.ms-listviewtable[summary='"+listTitle+"']>tbody>tr").each(function(){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $(this).append("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub'>");&nbsp; &nbsp; });&nbsp; &nbsp; $("table.ms-listviewtable[summary='"+listTitle+"']>tbody>tr input[value='Help']").click(function(){&nbsp; &nbsp; &nbsp; &nbsp; openMail($(this),subjectType);&nbsp; &nbsp; });}function openMail(btn,subjectType){&nbsp; &nbsp; var emailString = "mailto:";&nbsp; &nbsp; var emailID = $(btn).prev("td").text()&nbsp; &nbsp; //console.log(emailID);&nbsp; &nbsp; console.log('openMail:subjectType='+subjectType);&nbsp; &nbsp; emailString += emailID ;&nbsp; &nbsp; emailString += "?Subject=SharePoint Site Support - Site=";&nbsp; &nbsp; emailString += _spPageContextInfo.webServerRelativeUrl;;&nbsp; &nbsp; //alert(emailString);&nbsp; &nbsp; location.href=emailString;}

梦里花落0921

我建议向click按钮添加一个处理程序,而不是使用该onclick属性,以便您可以明确地传入所需的参数。那是:const button = $("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub'>");button.click(() => openMail(button, subjectType));$(document).ready(function() {&nbsp;&nbsp;&nbsp; &nbsp; // Get the subject type&nbsp; &nbsp; var this_js_script = $('script[src*=AddContactButtons]');&nbsp; &nbsp; var subjectType = this_js_script.attr('data-Subject');&nbsp;&nbsp; &nbsp; if (typeof subjectType == 'undefined' || subjectType == null || subjectType == ''){&nbsp; &nbsp; &nbsp; subjectType = "Site";&nbsp; &nbsp; }&nbsp; &nbsp; //console.log('subjectType='+subjectType);&nbsp; &nbsp; addContactButtons(subjectType);});function addContactButtons(subjectType){&nbsp; &nbsp; var listTitle="Contacts";&nbsp; &nbsp; console.log('addcontactButtons:subjectType='+subjectType);&nbsp; &nbsp; $(".container").each(function(){&nbsp; &nbsp; &nbsp; const button = $("<input type='button' value='Help' style='background-color:#0072C5; color:white' class='btnSub'>");&nbsp; &nbsp; &nbsp; button.click(() => openMail(button, subjectType));&nbsp; &nbsp; &nbsp; $(this).append(button);&nbsp; &nbsp; });}function openMail(btn, subjectType){&nbsp; console.log('openMail:subjectType='+subjectType);&nbsp; /*&nbsp; var emailString = "mailto:";&nbsp; var emailID = $(btn).prev("td").text()&nbsp; //console.log(emailID);&nbsp; console.log('openMail:subjectType='+subjectType);&nbsp; emailString += emailID ;&nbsp; emailString += "?Subject=SharePoint Site Support - Site=";&nbsp; emailString += _spPageContextInfo.webServerRelativeUrl;;&nbsp; //alert(emailString);&nbsp; location.href=emailString;&nbsp; */}<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 所说,您当前实施的内容存在范围和上下文问题。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答