Javascript 制造H5P含量
有人能解释什么是原型,以及我如何访问输入字段中键入的值吗?(例如InputSwapFirst)。 我试图生成一些代码来教人们使用H5p的快速排序算法Javascript 制造H5P含量,javascript,jquery,Javascript,Jquery,有人能解释什么是原型,以及我如何访问输入字段中键入的值吗?(例如InputSwapFirst)。 我试图生成一些代码来教人们使用H5p的快速排序算法 var H5P = H5P || {}; H5P.QuickSort = (function ($) { function C(options, id) { // Extend defaults with provided options this.options = $.extend(true, {}, {
var H5P = H5P || {};
H5P.QuickSort = (function ($) {
function C(options, id) {
// Extend defaults with provided options
this.options = $.extend(true, {}, {
toSort: '42'
}, options);
// Keep provided id.
this.id = id;
this.list = this.options.toSort.split(',').map(x =>+x);
this.list = quickSort(this.list, 0, this.list.length - 1);
};
* @param {jQuery} $container
C.prototype.attach = function ($container) {
$container.addClass("QuickSort");
$container.append('<div class="greeting-text">' + this.options.toSort+testM()+ '</div>');
$container.append('<div class="greeting-text">' + this.list+ '</div>');
$container.append('<button id="ButtonSwap" class="inputSwap" type="button" id="swapBtn" onclick="testM()">Swap</button>');
$container.append('<label id="InputSwapLeftBracket" class="inputSwap" for="swap">(</label> ');
$container.append('<input id="InputSwapFirst" class="inputSwap" type="text"/>');
$container.append('<label id="InputSwapComma" class="inputSwap" for="partition">,</label> ');
$container.append('<input id="InputSwapSecond" class="inputSwap" type="text"/>');
$container.append('<label id="InputSwapRightBracket" class="inputSwap" for="partition">)</label> ');
$container.append('<button id="ButtonPart" class="inputPart" type="button" id="partBtn">Partitioniere</button>');
$container.append('<label id="InputPartLeftBracket" class="inputPart" for="partition">(</label> ');
$container.append('<input id="InputPartFirst" class="inputPart" type="text"/>');
$container.append('<label id="InputPartComma" class="inputPart" for="partition">,</label> ');
$container.append('<input id="InputPartSecond" class="inputPart" type="text"/>');
$container.append('<label id="InputPartRightBracket" class="inputPart" for="partition">)</label> ');
};
return C;
})(H5P.jQuery);
var H5P=H5P |{};
H5P.QuickSort=(函数($){
功能C(选项,id){
//使用提供的选项扩展默认值
this.options=$.extend(true,{}{
toSort:'42'
},选项);
//保留提供的身份证。
this.id=id;
this.list=this.options.toSort.split(',).map(x=>+x);
this.list=快速排序(this.list,0,this.list.length-1);
};
*@param{jQuery}$container
C.prototype.attach=函数($container){
$container.addClass(“快速排序”);
$container.append(“”+this.options.toSort+testM()+“”);
$container.append(“”+this.list+“”);
$container.append('Swap');
$container.append(“(”);
$container.append(“”);
$container.append(',');
$container.append(“”);
$container.append(');
$container.append('Partitioniere');
$container.append(“(”);
$container.append(“”);
$container.append(',');
$container.append(“”);
$container.append(');
};
返回C;
})(H5P.jQuery);
原型
“原型”是JavaScript的一个(或)概念。您可以将其视为所有JavaScript对象都具有的属性,可以公开访问,并且该对象的后代可以继承。我认为这是一个很好的介绍——但我认为现在几乎没有人会用这种方式编写代码。不过,当然,了解这个概念还是很好的
从输入字段中检索值
在上面的代码中,使用jQuery附加子对象。您必须掌握InputSwapFirst
元素,有几种方法。如果出于某种原因您不想存储jQuery元素/HTMLElements,那么您可以使用如下查询:
// plain JavaScript, gives you the HTMLElement from DOM
const foo = document.querySelector('#InputSwapFirst');
或
或
但是,您可以在将该元素添加到DOM之前创建该元素并将其存储在变量中(这里是foo
和$foo
),从DOM检索元素往往会变得混乱。使用纯JavaScript或jQuery其实并不重要
然后,您可以简单地访问文本输入字段的value属性,在这里将其打印到控制台:
// Plain JavaScript
const foo = document.querySelector('#InputSwapFirst'); // Assuming that field can in fact be found - could also return null!
console.log(foo.value);
或
甚至
// jQuery using its chaining capabilities and not storing the variable
console.log($('#InputSwapFirst').val());
进一步建议
如果你想坚持使用jQuery,那就这么做吧——它有它的好处。但我认为它已经失去了相当大的吸引力,从H5P核心和内容类型中删除所有jQuery依赖关系是有可能的
Mozilla开发者网络是一个关于HTML、DOM以及两者与JavaScript结合使用的极好资源,例如在input
元素上:
首先可能会觉得有点吓人,但您可能想看看H5P模板。它的主分支为简单内容类型提供了一个完整的基本示例,正如您在上面概述的那样,还有一个分支为问题类型内容类型,位于这是否回答了您的问题?另见。请在提问前做些调查。请不要回答以前明显被问过的问题。堆栈溢出已经存在10多年了,关于如何在JavaScript中执行X,几乎每个方面都存在问题。如果您对该主题有新的看法,请务必为这些重复的问题添加答案,但要事先警告他们确实需要覆盖新的领域或以独特的方式覆盖它。@HereticMonkey谢谢您的提示!将来也会这样。
// Plain JavaScript
const foo = document.querySelector('#InputSwapFirst'); // Assuming that field can in fact be found - could also return null!
console.log(foo.value);
// jQuery
const $foo = $('#InputSwapFirst');
console.log($foo.val());
// jQuery using its chaining capabilities and not storing the variable
console.log($('#InputSwapFirst').val());