gpt4 book ai didi

javascript - 我可以将多个 JavaScript 对象方法保存为变量吗?

转载 作者:行者123 更新时间:2023-12-02 15:43:25 25 4
gpt4 key购买 nike

我正在为文本编辑器(Brackets)编写一个扩展,它可以生成 HTML 并在 HTML 中自动附加库。

我有一个名为“选择”的对象。

此模式请求用户输入: Imgur

choice 通过在 choice 上定义方法来获取用户的输入

此处部分 JS:

var choice = new Object();

choice.language = function () {
//Buid HTML top 'head'
var htmlTop = "<!DOCTYPE html>" + "<html>" + "<head lang='";
//Grab Selected Language Type
var languageChoice = document.getElementById("languages").value;
//Determine what Selected Language type is and complete HTML 'head'
if (languageChoice === "english") {
languageChoice = "en";
return htmlTop + languageChoice + "'>";
} else if (languageChoice === "german") {
languageChoice = "de";
return htmlTop + languageChoice + "'>";
} else if (languageChoice === "spanish") {
languageChoice = "es";
return htmlTop + languageChoice + "'>";
} else if (languageChoice === "french") {
languageChoice = "fr";
return htmlTop + languageChoice + "'>";
} else if (languageChoice === "italian") {
languageChoice = "it";
return htmlTop + languageChoice + "'>";
} else if (languageChoice === "chinese") {
languageChoice = "zh-cn";
return htmlTop + languageChoice + "'>";
}
}; //end choice.language

choice.charset = function () {
//Build meta and the rest of the 'head tag'
var htmlCharset_Beginning = "<meta charset='";
var htmlCharset_End = "'>" + "<title> -Insert Title- </title>" + "<!-- Insert CSS links below -->" + "</head>" + "<body>";
var charsetChoice = document.getElementById("charset").value;
if (charsetChoice === "utf8") {
charsetChoice = "UTF-8";
return htmlCharset_Beginning + charsetChoice + htmlCharset_End;
} else {
charsetChoice = "UTF-16";
return htmlCharset_Beginning + charsetChoice + htmlCharset_End;
}
}; // end choice.charset

choice.doctype = function () {
var doctypeChoice = document.getElementById("doctype").value;
return doctypeChoice;
}; // end doctype

choice.libraries = function () {
var checkedBoxes = getCheckedBoxes("lib_checkboxes");
checkedBoxes.forEach(function(item){
var scripts =+ $(item).data('script');

});//End forEach
var bottomHTML = scripts + "</body>" + "</html>";
return bottomHTML;
}; //End choice.libraries


var chosenTemplate = function(){
var template = choice.language() + choice.charset() + choice.libraries();

// insert html into file, this will overwrite whatever content happens to be there already
EditorManager.getCurrentFullEditor()._codeMirror.setValue(template);

// automatically close the modal window
$('#templates_modalBtn').click();
};

//Get checkedBoxes function
// Pass the checkbox name to the function
function getCheckedBoxes(chkboxName) {
var checkboxes = document.getElementsByName(chkboxName);
var checkboxesChecked = [];
// loop over them all
for (var i = 0; i < checkboxes.length; i++) {
// And stick the checked ones onto an array...
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i]);
}
}
// Return the array if it is non-empty, or null
return checkboxesChecked.length > 0 ? checkboxesChecked : null;
}


} // End action();

//JEFF STOP CODING HERE

// Register the commands and insert in the File menu
CommandManager.register(Strings.MENU_COMMAND, 'templates', action);
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuDivider();
menu.addMenuItem('templates');

}); //end define;
<小时/>

问题:

我可以将多个方法(每个方法返回一个字符串)保存为变量吗?

示例如下:

  var chosenTemplate = function(){
var template = choice.language() + choice.charset() + choice.libraries();

// insert html into file, this will overwrite whatever content happens to be there already
EditorManager.getCurrentFullEditor()._codeMirror.setValue(template);

// automatically close the modal window
$('#templates_modalBtn').click();
};
<小时/>

我的代码加载时没有错误,但它根本没有执行,所以我正在尝试调试并找出出了什么问题......

最佳答案

在实现“chosenTemplate”功能之前,应该检查页面的文档流是否已经下载。如果不是,您可能无法获取小部件的值(空)。

关于javascript - 我可以将多个 JavaScript 对象方法保存为变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32419126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com