jQueryのヒントとコツ 質問する

jQueryのヒントとコツ 質問する

構文

データストレージ

最適化

その他

ベストアンサー1

HTML要素を作成し参照を保持する

var newDiv = $("<div />");

newDiv.attr("id", "myNewDiv").appendTo("body");

/* Now whenever I want to append the new div I created, 
   I can just reference it from the "newDiv" variable */


要素が存在するかどうかを確認する

if ($("#someDiv").length)
{
    // It exists...
}


独自のセレクターを作成する

$.extend($.expr[":"], {
    over100pixels: function (e)
    {
        return $(e).height() > 100;
    }
});

$(".box:over100pixels").click(function ()
{
    alert("The element you clicked is over 100 pixels height");
});

おすすめ記事