简体   繁体   English

在ItemView Marionette.js中使用jquery获取ui元素的正确方法

[英]Proper way to get ui element with jquery inside a ItemView Marionette.js

I'm learning Marionette.js and would like know the correct way to get the ui element to manipulate it with jQuery. 我正在学习Marionette.js,并想知道使用jQuery操作ui元素的正确方法。 In my LoginItemView I'm declaring the ui elements and an function to display a error message of invalid login: 在我的LoginItemView中,我声明了ui元素和一个显示无效登录错误消息的函数:


ui: {
  username: "#username",
  password: "#password",
  btnLogin: "#btnDoLogin",
  messageContainer: "#messageContainer"
},
displayMessage: function() {
  // show error message
  $(this.ui.messageContainer.selector).show();
},

I also tried: 我也尝试过:


  $(this.ui.messageContainer[0]).show();

but the message is never displayed. 但邮件永远不会显示。
And here is the code of containerMessage in the template. 这是模板中的containerMessage代码。

<div class="alert alert-danger alert-dismissable login-message-display" id="#messageContainer" style="display: none;">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    <strong>Error!</strong> Username and/or password incorrect!
</div>

There is no need to add selector on the ui element. 无需在ui元素上添加selector ui is a simple object to map the selectors to keys. ui是一个将选择器映射到键的简单对象。

Just use 只是用

$(this.ui.messageContainer).show();

First of all be sure that you manipulate with DOM elements after view is rendered. 首先要确保在渲染视图后使用DOM元素进行操作。

Second - be sure that displayMessage is caled with proper context, i mean this in linked with view instanse. 二-确保displayMessage是caled适当情况下,我在视图instanse链接这个意思。

Third - use this syntax provided by @Billy Chan to operate with node. 第三 - 使用@Billy Chan提供的语法来操作节点。

If you need further assist - please create fiddle for tests. 如果您需要进一步的帮助 - 请为测试创建小提琴。

It is very Simple to use ui element as jquery object 将ui元素用作jquery对象非常简单

Use this.ui.messageContainer.show()

this.ui.messageContainer will itself return the jquery object. this.ui.messageContainer本身将返回jquery对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM