-->

2011-10-13

jqueryのbind以外でclickに引数をもたせる

参考URL。
http://www.tinybeans.net/blog/2009/02/19-134819.html
http://semooh.jp/jquery/api/events/bind/type,+%5Bdata%5D,+fn/

jqueryのbind以外でclickに引数をもたせる。
$(xxx).click((function(key){return function(){
    ...
};})(key));
追記: ループの中で使うと遅くなりそうなのでbind("click" ...)の方が良いです。

例。


ソース。
<style type="text/css">
<!--
body #click-test-ap8cljwu, body #click-test-ap8cljwu * {
  background-color: white;
  border-width: 0;
  color: black;
  font-family: "MS ゴシック","Osaka-mono",monospace;
  font-size: 14px;
  font-style: normal;
  font-variant: normal;
  font-weight: normal;
  letter-spacing: 0;
  line-height: 14px;
  margin: 0;
  padding: 0;
  text-align: center;
  text-indent: 0;
  text-transform: none;
  white-space: nowrap;
  word-spacing: 0;
  word-wrap: normal;
}
body #click-test-ap8cljwu {
  margin: 0 auto;
  width: 336px;
}
body #click-test-ap8cljwu .click-test-ap8cljwu-list {
  height: 21px;
  margin-left: 7px;
  text-align: left;
}
body #click-test-ap8cljwu .click-test-ap8cljwu-list ul {
  background-color: transparent;
  position: absolute;
}
body #click-test-ap8cljwu .click-test-ap8cljwu-list li {
  display: block;
  float: left;
  margin-right: 7px;
  _width: 1px;
}
body #click-test-ap8cljwu .click-test-ap8cljwu-list a {
  background-color: #D3D3D3;
  border: 1px solid black;
  cursor: pointer;
  display: block;
  height: 14px;
  line-height: 14px;
  padding: 3px 7px;
}
body #click-test-ap8cljwu .click-test-ap8cljwu-list a.selected {
  background-color: white;
  border: 1px solid black;
  border-bottom-color: white;
}
body #click-test-ap8cljwu .click-test-ap8cljwu-result {
  border: 1px solid black;
  clear: both;
  height: 280px;
  overflow: auto;
  padding: 7px;
  text-align: left;
}
body #click-test-ap8cljwu .click-test-ap8cljwu-list ul {
  border-bottom-width: 0;
  list-style-type: none;
  margin: 0;
  padding: 0;
}
body #click-test-ap8cljwu .click-test-ap8cljwu-list ul li {
  border-top-width: 0;
  float: left;
  line-height: 14px;
  margin-top: 0;
  padding: 0;
}
-->
</style>
<div id="click-test-ap8cljwu"></div>
<script type="text/javascript">
//<![CDATA[<!--
try{(function(){
    var key, aElem, liElem,
    id = "click-test-ap8cljwu",
    list = {
        "one": ["ホーム", "1番", "ファースト"],
        "two": ["お知らせ", "2番", "セカンド"],
        "three": ["お問い合せ", "3番", "サード"]
    },
    listElem = document.createElement("div"),
    ulElem = document.createElement("ul"),
    resultElem = document.createElement("div");
    $(listElem).addClass(id + "-list");
    $(resultElem).addClass(id + "-result");
    $("#" + id).append(listElem);
    $(listElem).append(ulElem);
    $("#" + id).append(resultElem);
    for (key in list)
    {
        liElem = document.createElement("li");
        aElem = document.createElement("a");
        $(liElem).append(aElem);
        $(aElem).text(list[key][0]);
        $(aElem).click((function(key){return function(){
            var x;
            $("#" + id + " ." + id + "-list a").removeClass("selected");
            $(resultElem).text("");
            $(resultElem).append("key = " + key + "<" + "br/>");
            for (x = 0; x < list[key].length; x++)
            {
                $(this).addClass("selected");
                $(resultElem).append("list[" + key + "][" + x + "] = " + list[key][x] + "<" + "br/>");
            }
        };})(key));
        $(ulElem).append(liElem);
        $("#" + id + " ." + id + "-list a").each(function(){
            $(this).click();
            return false;
        });
    }
})();}catch(e){document.write("<"+"pre>Error: " + e + "<"+"/pre>");}
//-->]]>
</script>

0 件のコメント: