- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我刚开始使用 backbone.js,我注意到的一件事是有时我不想要任何 tagName
包含/封装我的 View 的模板代码。如果我把它留在 ''
或 'span'
,我变得不必要div
和 span
在我的代码中。
我发现的替代方法是从我的模板中删除包含标签(在我的示例中为 <div class="photo_box">
,如下所示),并将其用作 tagName
在我看来。大多数时候,这个标签会包含一个类(.photo_box
),我仍然需要执行一个addClass
。到(this.el)。我真的不喜欢分散我的模板代码。
还有别的办法吗?
JS
// Views
PhotoListView = Backbone.View.extend({
tagName: 'span',
render: function() {
_.each(this.model.models, function(photo) {
$(this.el).append(new PhotoListItemView({ model: photo }).render().el);
}, this);
return this;
}
});
PhotoListItemView = Backbone.View.extend({
tagName: 'span',
template: _.template($('#tpl-PhotoListItemView').html()),
render: function() {
$(this.el).html(this.template( this.model.toJSON() ));
return this;
}
});
HTML
<!-- Templates -->
<script type="text/template" id="tpl-PhotoListItemView">
<div class="photo_box">
<div class="photo_container">
<img src="img/profiling/<%= photo_id %>.jpg" class='photo' />
</div>
</div>
</script>
结果
<div id="photo_list">
<span>
<span>
<div class="photo_box">
<div class="photo_container">
<img src="img/profiling/f_001.jpg" class="photo">
</div>
</div>
</span>
<span>
<div class="photo_box">
<div class="photo_container">
<img src="img/profiling/f_002.jpg" class="photo">
</div>
</div>
</span>
</span>
</div>
最佳答案
你总是可以使用 setElement
:
setElement
view.setElement(element)
If you'd like to apply a Backbone view to a different DOM element, use setElement, which will also create the cached
$el
reference and move the view's delegated events from the old element to the new one.
忘记tagName
完全:
PhotoListItemView = Backbone.View.extend({
template: _.template($('#tpl-PhotoListItemView').html()),
render: function() {
this.setElement(this.template(this.model.toJSON()));
return this;
}
});
演示:http://jsfiddle.net/ambiguous/XWEMg/
顺便说一句, <span>
由于其有限的 permitted content ,对于容器(即使是临时容器)来说是一个糟糕的选择;如果您开始将任意 HTML 放入 <span>
中,您冒着浏览器重新排列 HTML 以获得有效内容的风险. <div>
是一个更安全的选择,因为它几乎可以容纳任何东西。
关于javascript - Backbone.js 我们可以没有任何 tagName 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11178816/
是否有内置函数(我知道我可以轻松创建一个)来创建简单的标签: MakeTag("Tag","myvalue") 会生成: myvalue 我宁愿使用比 XML 类更简单的东西,我所需要的只是一个像这样
我在尝试解析 HTML 文档时遇到了以下情况。我把内容以字符串的形式放到了下面的代码中。在此, anchor 标记内有一个 P 标记。如果使用 Jsoup 解析,它会在 #item1 附近添加额外的
if (((document.activeElement).parentNode).tagName == "div") { //do amazing things } 出于某种原因,我的某个特定
基本上我试图提取一些不同的 css 选择器的最后一个标签名称。 我已经在 javascript 中成功地实现了我正在谈论的内容,我正在寻找一种更紧凑的方式,最好只使用一个正则表达式。 这是我成功完成的
我在一些元素上运行一个循环,并尝试使用我在循环中获得的 id 访问另一组元素我尝试引用其他元素并获取它们的标签,这是我的代码。 function checkRequired(){ var i
我想创建一个函数来达到以下效果: function supportsElem(tagName) { // returns boolean } 哪里: supportsElem("div") //
我需要读取文件并查找并替换同一文件中的某些文本,而无需关闭阅读器。 建议任何简单的方法... 我正在读取文件,如下所示。我怎样才能将它写入同一个文件。 private static void read
我需要从我点击文档的任何元素中获取 $('this') 信息。 我尝试了以下代码: $('body').click(function(){ var element = this.tagName
我正在尝试从网站上提取一些数据,但由于我是网络抓取的新手,因此在标签名称、类代码和 ID 中感到困惑。我对此只有基本知识。 我想复制下面的数据,如果数据不存在,那么单元格应该留空,代码需要移入下一个值
我正在尝试根据条件切换 Backbone View 的 tagName。 我最初认为我可以设置一个默认的 tagName 为 'div'(我意识到这是默认的),然后在 View 的初始化函数中,检查条
我正在编写一个 iPhone 应用程序,它使用一点 JavaScript 代码来获取 UIWebView 上某个点的元素。 我有一点 iPhone 经验,但没有 javascript 经验,我在教程上
对于以下代码,element.tagName 未定义。 $('#page1').bind('pageinit', function(event) { $('form').validate({
Example Fiddle 。 我需要添加条件,以便我的代码支持输入(需要 1 个按钮生成整个 Json)。 我尝试过过滤: elements.tagName 但它给了我一个未定义的,我想这是因为
这个问题已经有答案了: How to fix error: The markup in the document following the root element must be well-for
原始问题 是否有一个 jQuery 方法可以检查选择类型并适本地设置值?例如,使用 .html() 或 .val()。 我已经制定了自己的方法来为我完成这项工作,但不确定这是否是执行此任务的最佳方法。
我有这个 jQuery 代码: $('*').mouseover(function() { $('#log').text($(this)[0].tagName);
我有我的html Hello @samplename This is my comment ... ... More .comment-details elements 在页面加载时,我想在 .com
我在 javascrip 中有一个小型模板系统,用户可以在其中以 $tagname$ 的形式放置标签。我可以将所有标签与模式匹配:/\$\w+\$/。 此外,我想专门匹配不完整的标签(它将以 $ 开头
.tagName 属性是否适用于直接元素?我一直在使用子元素作为起点让它工作,但我无法让它在直接元素上工作。 这是一个有效的例子: HTML This is paragraph 1. This is
我有以下 HTML 片段: Jill Martin rescues Savannah Guthrie from her guest
我是一名优秀的程序员,十分优秀!