1

我有这个滑块交互,我想在任何地方折叠或展开 div,除非他们单击那个大 div 中的链接,然后我只希望链接在没有任何折叠/展开的情况下工作。问题是, .is() (看看我在那里做了什么?)。

我正在使用 jquery。

这是代码(对不起,它在coffeescript中,但我懒得反向翻译):

$ ->
  # SLIDER INTERACTION
  $(".page-content").click (e) ->
    # This first part is just because ie only recognizes srcElement, not target
    if e.target
      $targ = e.target
    else $targ = e.srcElement  if e.srcElement

    # Now I check to see if they clicked a link, if so, follow the link
    if $targ.is("a")
      true

    # If they clicked a collapsed profile, expand it
    else if $(this).hasClass(".collapsed")
      $(this).css "margin-bottom": "0px"
      $(this).removeClass ".collapsed"
      $(this).find(".review-btns, .section-header, .dl-horizontal").show()

    # Otherwise, collapse it
    else
      $(this).css "margin-bottom": "20px"
      $(this).addClass ".collapsed"
      $(this).find(".review-btns, .section-header, .dl-horizontal").hide()

现在这对我来说似乎很可靠,但是当我单击任何内容时,我会在控制台中收到此错误:

Uncaught TypeError: Object #<HTMLDivElement> has no method 'is'

我一直用头撞桌子。这可能是愚蠢的,我在这里超出了我的深度。

在此先感谢,马特

4

3 回答 3

2

我不知道coffeescript,但很明显你的错误源于$targ一个DOM元素,而不是一个jQuery对象(因此没有is方法)。我相信要修复它,您只需要包装它:

if e.target
  $targ = $(e.target)
else $targ = $(e.srcElement)  if e.srcElement

(我相信这是正确的方法,在这个网站上看到其他例子,但如果不是,我恐怕无法帮助你......)

于 2013-02-21T05:23:09.800 回答
1

而不是$targ = e.target使用$targ = $(e.target)

或者您可以将 if 条件更新为

if $($targ).is("a")
      true
于 2013-02-21T05:23:03.497 回答
1

快速浏览一下,您似乎没有将目标作为 jQuery 对象及其方法。

尝试改变

if e.target
  $targ = e.target
else $targ = e.srcElement  if e.srcElement

if e.target
  $targ = $(e.target)
else $targ = $(e.srcElement)  if e.srcElement

这应该将 $targ 设置为被引用节点的实际 jquery 对象。

于 2013-02-21T05:28:16.587 回答