如何从iframe.contents()中选择父元素?

时间:2014-03-03 15:14:46

标签: javascript jquery html iframe

我想调用一个函数,当我点击一个iframe时,我的解决方案在jQuery上工作得很好,但是当触发函数时我需要回到包含iframe的元素。

$('#window-3 iframe').load(function () {
  $(this).contents().find("body").click(function () {
    // Here I need to select the iframe again
  });
});

1 个答案:

答案 0 :(得分:1)

在这种情况下,您需要$(this).parent()。如果要选择具有特定类的父级,则可以使用$(this).parents('#window-3')

您可能还想澄清任何潜在的范围问题:

$('#window-3 iframe').load(function () {
  var _me = $(this);

  _me.contents().find("body").click(function () {
    var _parent = _me.parent()
    // Here I need to select the iframe again
  });
});