使用小胡子模板和实现无限滚动时出现 DOM 异常 8 错误jquery砌体

发布于 2024-12-26 22:55:54 字数 1493 浏览 4 评论 0原文

我正在实现一个深受本教程影响的无尽滚动效果:

http:// railscasts.com/episodes/295-sharing-mustache-templates?view=asciicast

但是,我的做法略有不同,因为我使用的是 Jquery Masonry (http://masonry.desandro.com/demos/adding-items.html) 在前端。

无论如何,当我按如下方式实现时:

jQuery ->
window.endlessScroll = () ->
    if $('#products_page').length   
        new ProductPager

class ProductPager
constructor: ->
    $(window).scroll(@check)

check: =>
    if @nearBottom()
        $(window).unbind('scroll', @check)
        $.getJSON($('#products_page').data('json-url'), @render)

nearBottom: =>
    $(window).scrollTop() > $(document).height() - $(window).height() - 150

render: (products) =>
    boxes= []
    $container = $('#products_page')
    for product in products
        boxes.push Mustache.render $('#mustache_product').html(), product 
    $container.append(boxes).masonry "appended", boxes
    $(window).scroll(@check)

我收到以下错误(Chrome):

未捕获错误:NOT_FOUND_ERR:DOM异常8

我认为问题出在这里:

boxes.push Mustache.render $('#mustache_product').html(), product

因为这将每个模板输出包装在“引号”中,而

["<div>stuff</div>","<div>more stuff</div>"]

不是:

[<div>stuff</div>,<div>more stuff</div>]

但我是对我做错的事情有一点心理障碍......有人愿意帮忙吗?

I am implementing an endless scrolling effect heavily influenced by this tutorial:

http://railscasts.com/episodes/295-sharing-mustache-templates?view=asciicast

However, I am doing it slightly differently because I am using Jquery Masonry (http://masonry.desandro.com/demos/adding-items.html) on the front end.

Anyway, when I implement this as follows:

jQuery ->
window.endlessScroll = () ->
    if $('#products_page').length   
        new ProductPager

class ProductPager
constructor: ->
    $(window).scroll(@check)

check: =>
    if @nearBottom()
        $(window).unbind('scroll', @check)
        $.getJSON($('#products_page').data('json-url'), @render)

nearBottom: =>
    $(window).scrollTop() > $(document).height() - $(window).height() - 150

render: (products) =>
    boxes= []
    $container = $('#products_page')
    for product in products
        boxes.push Mustache.render $('#mustache_product').html(), product 
    $container.append(boxes).masonry "appended", boxes
    $(window).scroll(@check)

I get the following error (Chrome):

Uncaught Error: NOT_FOUND_ERR: DOM Exception 8

I think the problem lies here:

boxes.push Mustache.render $('#mustache_product').html(), product

because this wraps each template output in "quotes" i.e.

["<div>stuff</div>","<div>more stuff</div>"]

rather than:

[<div>stuff</div>,<div>more stuff</div>]

But I am having a bit of a mental block about what I am doing wrong.... anyone care to help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2025-01-02 22:55:55

我通过更仔细地遵循 Masonry 网站上的示例来实现此目的,结果是:

.
.
.
boxMaker.makeBoxes = ->
  boxes = []
  for product in products
    box = document.createElement("div")
    template = Mustache.render $('#mustache_product').html(), product
    box.className = "box"
    box.innerHTML = template
    boxes.push box
  boxes
$boxes = $(boxMaker.makeBoxes())

I got this to work by following the example on the Masonry website more closely, which resulted in this:

.
.
.
boxMaker.makeBoxes = ->
  boxes = []
  for product in products
    box = document.createElement("div")
    template = Mustache.render $('#mustache_product').html(), product
    box.className = "box"
    box.innerHTML = template
    boxes.push box
  boxes
$boxes = $(boxMaker.makeBoxes())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文