【问题标题】:Get jQuery Element from currentView从 currentView 获取 jQuery 元素
【发布时间】:2015-04-18 14:00:45
【问题描述】:

在 api 文档中,可以使用选择器找到当前模板实例视图,因为选择器的属性是提前知道的。

template.findAll(selector)
template.$(selector)

我也知道可以从 Blaze.currentView 或 template.view 获取视图,但是是否可以从 Blaze.currentView 或 template.view 获取 jQuery 元素?我想这样做是因为我事先不知道模板实例的属性。

【问题讨论】:

  • 我想我需要更多关于你到底想要做什么的信息。我不确定“视图的 jQuery 元素”是什么意思。

标签: meteor meteor-blaze


【解决方案1】:

您可以使用以下方法访问绑定到模板实例或视图的第一个和最后一个 DOM 节点:

  • 模板实例上的 firstNode 和 lastNode 属性。
  • Blaze.View 实例上的 firstNode() 和 lastNode() 方法。

当您检索一个 DOM 元素时,您可以像往常一样使用 $ 函数从它构建一个 jQuery 对象。 HTML

<template name="test">
  <p>First paragraph</p>
  <p>Last paragraph</p>
</template>

JS

Template.test.rendered=function(){
  var $firstNode=$(this.firstNode);
  $firstNode.css("backgroundColor","red");
  //
  var $lastNode=$(this.view.lastNode());
  $lastNode.css("backgroundColor","green");
};

在这里查看Blaze.View 的完整文档:http://docs.meteor.com/#/full/blaze_view

【讨论】:

    猜你喜欢
    • 2013-06-30
    • 1970-01-01
    • 2022-12-05
    • 2014-10-31
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    相关资源
    最近更新 更多