[英]meteor.js: How to access value of HTML-element
Using the meteor.js framework, how can the value of a HTML element be selected in the "meteor way"? 使用meteor.js框架,如何以“流星方式”选择HTML元素的值? By using a jQuery-selector the browser would iterate through the DOM for every item, which is very expensive, wouldn't it? 通过使用jQuery选择器,浏览器将遍历每个项目的DOM,这非常昂贵,不是吗?
The meteor tutorial uses a submit form and handles the template variable in a onSubmit-event. 流星教程使用提交表单并在onSubmit事件中处理模板变量。 But how is it done if there is no onSubmit (and therefore no template-variable containing the element in question? 但是,如果没有onSubmit(因此没有包含所讨论元素的模板变量)怎么办?
Could someone help out with the following example given, please? 有人可以帮忙提供以下示例吗?
cars.html cars.html
<template name="Car">
<div class="car-item" contenteditable="true">BMW</div>
<div class="edit-bar"><a href="#" class="save">save</a></div>
</template>
cars.js cars.js
'click .save'(event, template){
//access content of '.car-item' here when '.save' is clicked
}
You can use the template instance's jQuery . 您可以使用模板实例的jQuery 。 It will scope only the elements of the current template: 它将仅限制当前模板的元素:
The template instance serves as the document root for the selector. 模板实例用作选择器的文档根。 Only elements inside the template and its sub-templates can match parts of the selector. 只有模板及其子模板中的元素才能匹配选择器的各个部分。
This results in a higher performance but requires you to control the granularity and scope of the elements to be searched. 这样可以提高性能,但是需要您控制要搜索的元素的粒度和范围。
Just compare the output of the follwing example: 只需比较以下示例的输出即可:
'click .save'(event, templateInstance){
//access content of '.car-item' here when '.save' is clicked
// global scope search
console.log($('div'));
// template scope search
console.log(templateInstance.$('div'));
}
it results in the following code: 结果为以下代码:
'click .save'(event, templateInstance){
// access content of '.car-item' here when '.save' is clicked
const carItems = templateInstance.$('.car-item');
// ... process car items
}
Try adding names to your divs 尝试将名称添加到div
<div class="car-item" contenteditable="true" name="car">
Then in your click event: 然后在您的点击事件中:
click.save(event, template){
var car = event.target.car.value;
}
Let us know if it worked. 让我们知道它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.