0

以下不起作用:

function doRestyle(div) {
        jQuery(div).css({
            "margin-top": addableMargin + "px",
            "margin-bottom": addableMargin + "px",
        });
}

但是,这有效:

function doRestyle(div) {
        jQuery('#idName').css({
            "margin-top": addableMargin + "px",
            "margin-bottom": addableMargin + "px",
        });
}

有什么解释吗?非常感谢 :)

4

2 回答 2

3

“有什么解释吗?”

Apparently when you call your function the argument you pass in (that becomes div) is not a string with the appropriate selector or a reference to the DOM element in question (or a jQuery object containing a reference to that element).

您需要像这样调用您的函数:

doRestyle("#idName");

或者使用一些已适当设置的变量:

var id = "idName";
doRestyle("#" + id);

或者也许在事件处理程序中:

$("#idName").click(function() {
    doRestyle(this);
});

等等。

于 2013-11-05T10:25:36.180 回答
0
function doRestyle(someID) {
        jQuery('#'+someID).css({
            "margin-top": addableMargin + "px",
            "margin-bottom": addableMargin + "px",
        });
}

:D

于 2013-11-05T10:34:06.373 回答