假设我有以下内容:
$('input[rel]').jOverlay({ overlayId: "#overlayID" });
如何从输入的rel值动态获取overlayID?我尝试过使用:
$('input[rel]').jOverlay({ overlayId: this.attr('rel') });
我尝试将其包装在匿名函数中:
$('input[rel]').jOverlay({ (function() { overlayId: this.attr('rel'); }) });
没有效果,正确的方法是什么?
非常感谢 - 杰伊
最佳答案
您必须使用 each()
迭代它们,否则 this
(在您的第一个和第二个示例中)实际上是 window
而不是元素(此外,如果 this
没有意义,您匹配了多个元素)。
$('input[rel]').each(function () {
// In here, this is the specific element in the
// jQuery set we're iterating over.
$(this).jOverlay({ overlayId: this.rel });
});
第三个不起作用的原因很可能是您使用的插件不支持该参数列表。
关于jquery - 如何在链式函数 init 调用中选择时获取对元素的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204524/