2012-05-07 33 views
1

使用jQuery和backbone.js,我想尽可能地从HTML中分离JavaScript。
是否可以添加custom attribute以更好地区分JavaScript和HTML? 然后我该如何使用新的jQuery选择器?添加自定义属性以更好地区分JavaScript和HTML

例子:

<input type="text" id="todo-input" class="todo-input" tid="tid-attribute" > hello world </input> 

// The DOM events 
// I would like to use another selector tid and not class or id 
events: { 
    "keypress #todo-input" : "updateOnEnter" // I would like to use another selector tid not id, 
    "keypress .todo-input" : "updateOnEnter" // I would like to use another selector tid and not class 
} 
+0

我还是不明白这一点。你想分离吗?或者属性会打扰你。据我所知,这是视图的工作,不要渲染骨干应用程序的任何服务器依赖。这将使您的骨干应用部署在任何地方。 – Deeptechtons

+0

HTML'class'属性用于各种事物,而不仅仅用于CSS样式。向元素添加额外的类只是为了在选择器中使用没有任何问题。 –

回答

3

HTML5允许与data-开始自定义数据属性:

<input type="text" data-custom-attr="somevalue"> 

然后在你的骨干事件的声明:

"keypress [data-custom-attr='somevalue']" : "updateOnEnter" 
0

你可以使用:

$('input[type="text"]').attr('tid', 'tid-attribute'); 

OR

$('input[type="text"]').attr('data-tid', 'tid-attribute');