0

希望我能正确解释这一点。

我试图不重复代码,并有一个循环将悬停功能绑定到显示大约 30 个模型的菜单中的特定模型。我希望能够轻松添加更多模型,所以我做了一个循环。

var models = ["#model1", "#model2", "#model3", "#model4", "#model5"];

for(var index = 0; index < models.length; ++index) {
   $(models[index]).hover(fadeInAndBlock, fadeOutAndUnblock);
}

现在这工作正常。是fadeInAndBlock 不能正常工作。我正在尝试让一些按钮亮起并阻止页面的其余部分。

function fadeInAndBlock() {
$(".productmenuinfo").block({
    overlayCSS: {
        backgroundColor: "#fff",
        opacity: 0.6,
        cursor: "default"
    },
    message: null
});
$(this).unblock( { fadeOut: 0});


$(this + " .btnproductmoreinfo").css({
    backgroundPosition: "0px 24px"
});
$(this + " .btnproductconfigure").css({
    backgroundPosition: "0px 24px"
});
}

基本上,我无法让“this”在选择器中工作。我需要它,因为选择器应该只是那个模型按钮。谢谢你的帮助!

4

2 回答 2

3

当你这样做this + " .btnproductconfigure"时,就像this.toString() + " .btnproductconfigure"结果一样[object Object] .btnproductconfigure

所以你需要使用你已经拥有的对象并find()用来获取你想要的元素。

所以这条线

$(this + " .btnproductconfigure").css({

应该

$(this).find(".btnproductconfigure").css({

此外,您真的不需要维护 id 列表。在所有元素上使用一个公共类,您将不必循环和维护列表。

于 2013-05-01T15:44:44.760 回答
2

您可以将此作为上下文传递给选择器以在后代中查找。

句法 jQuery( selector [, context ] )

$(".btnproductmoreinfo", this ).css({
    backgroundPosition: "0px 24px"
});
于 2013-05-01T15:46:54.373 回答